1 /*
2  * Copyright (C) 2021 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.SmartAutoRotateController.hasSufficientPermission;
20 import static com.android.settings.display.SmartAutoRotateController.isRotationResolverServiceAvailable;
21 
22 import android.content.Context;
23 import android.content.Intent;
24 import android.net.Uri;
25 
26 import androidx.preference.Preference;
27 import androidx.preference.PreferenceScreen;
28 
29 import com.android.settings.R;
30 import com.android.settings.core.BasePreferenceController;
31 import com.android.settingslib.core.lifecycle.LifecycleObserver;
32 import com.android.settingslib.core.lifecycle.events.OnResume;
33 import com.android.settingslib.widget.BannerMessagePreference;
34 
35 /**
36  * The controller of camera based rotate permission warning preference. The preference appears when
37  * the camera permission is missing for the camera based rotation feature.
38  */
39 public class SmartAutoRotatePermissionController extends BasePreferenceController implements
40         LifecycleObserver, OnResume {
41 
42     private final Intent mIntent;
43     private BannerMessagePreference mPreference;
44 
SmartAutoRotatePermissionController(Context context, String key)45     public SmartAutoRotatePermissionController(Context context, String key) {
46         super(context, key);
47         final String packageName = context.getPackageManager().getRotationResolverPackageName();
48         mIntent = new Intent(
49                 android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
50         mIntent.setPackage(context.getPackageName());
51         mIntent.setData(Uri.parse("package:" + packageName));
52     }
53 
54     @Override
displayPreference(PreferenceScreen screen)55     public void displayPreference(PreferenceScreen screen) {
56         super.displayPreference(screen);
57         mPreference = screen.findPreference(getPreferenceKey());
58         mPreference
59                 .setPositiveButtonText(R.string.auto_rotate_manage_permission_button)
60                 .setPositiveButtonOnClickListener(v -> {
61                     mContext.startActivity(mIntent);
62                 });
63     }
64 
65     @Override
onResume()66     public void onResume() {
67         updateState(mPreference);
68     }
69 
70     @Override
updateState(Preference preference)71     public void updateState(Preference preference) {
72         super.updateState(preference);
73         if (preference != null) {
74             preference.setVisible(isAvailable());
75         }
76     }
77 
78     @Override
79     @AvailabilityStatus
getAvailabilityStatus()80     public int getAvailabilityStatus() {
81         return isRotationResolverServiceAvailable(mContext) && !hasSufficientPermission(mContext)
82                 ? AVAILABLE_UNSEARCHABLE
83                 : UNSUPPORTED_ON_DEVICE;
84     }
85 }
86