• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.settings.slices;
18 
19 import android.content.Context;
20 import android.net.Uri;
21 import android.util.Log;
22 
23 import androidx.annotation.VisibleForTesting;
24 import androidx.lifecycle.LiveData;
25 import androidx.lifecycle.Observer;
26 import androidx.preference.PreferenceScreen;
27 import androidx.slice.Slice;
28 import androidx.slice.widget.SliceLiveData;
29 import androidx.slice.widget.SliceView;
30 
31 import com.android.settings.core.BasePreferenceController;
32 import com.android.settingslib.core.lifecycle.LifecycleObserver;
33 import com.android.settingslib.core.lifecycle.events.OnStart;
34 import com.android.settingslib.core.lifecycle.events.OnStop;
35 
36 /**
37  * Default {@link BasePreferenceController} for {@link SliceView}. It will take {@link Uri} for
38  * Slice and display what's inside this {@link Uri}
39  */
40 public class SlicePreferenceController extends BasePreferenceController implements
41         LifecycleObserver, OnStart, OnStop, Observer<Slice> {
42     private static final String TAG = "SlicePreferenceController";
43     @VisibleForTesting
44     LiveData<Slice> mLiveData;
45     @VisibleForTesting
46     SlicePreference mSlicePreference;
47     private Uri mUri;
48 
SlicePreferenceController(Context context, String preferenceKey)49     public SlicePreferenceController(Context context, String preferenceKey) {
50         super(context, preferenceKey);
51     }
52 
53     @Override
displayPreference(PreferenceScreen screen)54     public void displayPreference(PreferenceScreen screen) {
55         super.displayPreference(screen);
56         mSlicePreference = screen.findPreference(getPreferenceKey());
57     }
58 
59     @Override
getAvailabilityStatus()60     public int getAvailabilityStatus() {
61         return mUri != null ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
62     }
63 
setSliceUri(Uri uri)64     public void setSliceUri(Uri uri) {
65         mUri = uri;
66         mLiveData = SliceLiveData.fromUri(mContext, mUri, (int type, Throwable source) -> {
67             Log.w(TAG, "Slice may be null. uri = " + uri + ", error = " + type);
68         });
69 
70         //TODO(b/120803703): figure out why we need to remove observer first
71         mLiveData.removeObserver(this);
72     }
73 
74     @Override
onStart()75     public void onStart() {
76         if (mLiveData != null) {
77             mLiveData.observeForever(this);
78         }
79     }
80 
81     @Override
onStop()82     public void onStop() {
83         if (mLiveData != null) {
84             mLiveData.removeObserver(this);
85         }
86     }
87 
88     @Override
onChanged(Slice slice)89     public void onChanged(Slice slice) {
90         mSlicePreference.onSliceUpdated(slice);
91     }
92 }
93