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.setData(Uri.parse("package:" + packageName)); 51 } 52 53 @Override displayPreference(PreferenceScreen screen)54 public void displayPreference(PreferenceScreen screen) { 55 super.displayPreference(screen); 56 mPreference = screen.findPreference(getPreferenceKey()); 57 mPreference 58 .setPositiveButtonText(R.string.auto_rotate_manage_permission_button) 59 .setPositiveButtonOnClickListener(v -> { 60 mContext.startActivity(mIntent); 61 }); 62 } 63 64 @Override onResume()65 public void onResume() { 66 updateState(mPreference); 67 } 68 69 @Override updateState(Preference preference)70 public void updateState(Preference preference) { 71 super.updateState(preference); 72 if (preference != null) { 73 preference.setVisible(isAvailable()); 74 } 75 } 76 77 @Override 78 @AvailabilityStatus getAvailabilityStatus()79 public int getAvailabilityStatus() { 80 return isRotationResolverServiceAvailable(mContext) && !hasSufficientPermission(mContext) 81 ? AVAILABLE_UNSEARCHABLE 82 : UNSUPPORTED_ON_DEVICE; 83 } 84 } 85