1 /*
2  * Copyright (C) 2022 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.display;
18 
19 import android.content.Context;
20 import android.content.Intent;
21 import android.text.TextUtils;
22 
23 import androidx.preference.Preference;
24 import androidx.preference.PreferenceScreen;
25 
26 import com.android.settings.R;
27 import com.android.settings.core.BasePreferenceController;
28 import com.android.settings.core.PreferenceControllerMixin;
29 import com.android.settingslib.utils.ThreadUtils;
30 
31 /**
32  * Preference for accessing an experience to customize lock screen quick affordances.
33  */
34 public class CustomizableLockScreenQuickAffordancesPreferenceController extends
35         BasePreferenceController implements PreferenceControllerMixin {
36 
CustomizableLockScreenQuickAffordancesPreferenceController(Context context, String key)37     public CustomizableLockScreenQuickAffordancesPreferenceController(Context context, String key) {
38         super(context, key);
39     }
40 
41     @Override
getAvailabilityStatus()42     public int getAvailabilityStatus() {
43         return CustomizableLockScreenUtils.isFeatureEnabled(mContext)
44                 ? AVAILABLE
45                 : UNSUPPORTED_ON_DEVICE;
46     }
47 
48     @Override
displayPreference(PreferenceScreen screen)49     public void displayPreference(PreferenceScreen screen) {
50         super.displayPreference(screen);
51         final Preference preference = screen.findPreference(getPreferenceKey());
52         if (preference != null) {
53             preference.setOnPreferenceClickListener(preference1 -> {
54                 final Intent intent = CustomizableLockScreenUtils.newIntent();
55                 final String packageName =
56                         mContext.getString(R.string.config_wallpaper_picker_package);
57                 if (!TextUtils.isEmpty(packageName)) {
58                     intent.setPackage(packageName);
59                 }
60                 intent.putExtra("destination", "quick_affordances");
61                 mContext.startActivity(intent);
62                 return true;
63             });
64             refreshSummary(preference);
65         }
66     }
67 
68     @Override
refreshSummary(Preference preference)69     protected void refreshSummary(Preference preference) {
70         ThreadUtils.postOnBackgroundThread(() -> {
71             final CharSequence summary =
72                     CustomizableLockScreenUtils.getQuickAffordanceSummary(mContext);
73             ThreadUtils.postOnMainThread(() -> preference.setSummary(summary));
74         });
75     }
76 }
77