1 /*
2  * Copyright (C) 2019 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 static com.android.settings.display.AdaptiveSleepPreferenceController.hasSufficientPermission;
20 
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.pm.PackageManager;
24 import android.net.Uri;
25 
26 import androidx.preference.PreferenceScreen;
27 
28 import com.android.internal.annotations.VisibleForTesting;
29 import com.android.settings.R;
30 import com.android.settingslib.widget.BannerMessagePreference;
31 
32 /**
33  * The controller of Screen attention's permission warning preference. The preference appears when
34  * the camera permission is missing for Screen Attention feature.
35  */
36 public class AdaptiveSleepPermissionPreferenceController {
37     @VisibleForTesting
38     BannerMessagePreference mPreference;
39     private final PackageManager mPackageManager;
40     private final Context mContext;
41 
AdaptiveSleepPermissionPreferenceController(Context context)42     public AdaptiveSleepPermissionPreferenceController(Context context) {
43         mPackageManager = context.getPackageManager();
44         mContext = context;
45     }
46 
47     /**
48      * Adds the controlled preference to the provided preference screen.
49      */
addToScreen(PreferenceScreen screen)50     public void addToScreen(PreferenceScreen screen) {
51         initializePreference();
52         if (!hasSufficientPermission(mPackageManager)) {
53             screen.addPreference(mPreference);
54         }
55     }
56 
57     /**
58      * Refreshes the visibility of the preference.
59      */
updateVisibility()60     public void updateVisibility() {
61         initializePreference();
62         mPreference.setVisible(!hasSufficientPermission(mPackageManager));
63     }
64 
initializePreference()65     private void initializePreference() {
66         if (mPreference == null) {
67             final String packageName =
68                     mContext.getPackageManager().getAttentionServicePackageName();
69             final Intent intent = new Intent(
70                     android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
71             intent.setPackage(mContext.getPackageName());
72             intent.setData(Uri.parse("package:" + packageName));
73             mPreference = new BannerMessagePreference(mContext);
74             mPreference.setTitle(R.string.adaptive_sleep_title_no_permission);
75             mPreference.setSummary(R.string.adaptive_sleep_summary_no_permission);
76             mPreference.setPositiveButtonText(R.string.adaptive_sleep_manage_permission_button);
77             mPreference.setPositiveButtonOnClickListener(p -> {
78                 mContext.startActivity(intent);
79             });
80         }
81     }
82 
83 }
84