• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.applications.manageapplications;
18 
19 import android.content.Context;
20 import android.os.Bundle;
21 import android.telephony.TelephonyManager;
22 import android.text.TextUtils;
23 
24 import androidx.preference.Preference;
25 import androidx.preference.PreferenceScreen;
26 
27 import com.android.settings.core.PreferenceControllerMixin;
28 import com.android.settingslib.core.AbstractPreferenceController;
29 import com.android.settingslib.core.lifecycle.Lifecycle;
30 import com.android.settingslib.core.lifecycle.LifecycleObserver;
31 import com.android.settingslib.core.lifecycle.events.OnCreate;
32 import com.android.settingslib.core.lifecycle.events.OnSaveInstanceState;
33 
34 public class ResetAppPrefPreferenceController extends AbstractPreferenceController
35         implements PreferenceControllerMixin, LifecycleObserver, OnCreate, OnSaveInstanceState {
36 
37     private ResetAppsHelper mResetAppsHelper;
38 
ResetAppPrefPreferenceController(Context context, Lifecycle lifecycle)39     public ResetAppPrefPreferenceController(Context context, Lifecycle lifecycle) {
40         super(context);
41         mResetAppsHelper = new ResetAppsHelper(context);
42         if (lifecycle != null) {
43             lifecycle.addObserver(this);
44         }
45     }
46 
47     @Override
handlePreferenceTreeClick(Preference preference)48     public boolean handlePreferenceTreeClick(Preference preference) {
49         if (!TextUtils.equals(preference.getKey(), getPreferenceKey())) {
50             return false;
51         }
52         mResetAppsHelper.buildResetDialog();
53         return true;
54     }
55 
56     @Override
isAvailable()57     public boolean isAvailable() {
58         return true;
59     }
60 
61     @Override
getPreferenceKey()62     public String getPreferenceKey() {
63         return "reset_app_prefs";
64     }
65 
66     @Override
onCreate(Bundle savedInstanceState)67     public void onCreate(Bundle savedInstanceState) {
68         mResetAppsHelper.onRestoreInstanceState(savedInstanceState);
69     }
70 
71     @Override
onSaveInstanceState(Bundle outState)72     public void onSaveInstanceState(Bundle outState) {
73         mResetAppsHelper.onSaveInstanceState(outState);
74     }
75 
76     @Override
displayPreference(PreferenceScreen screen)77     public void displayPreference(PreferenceScreen screen) {
78         super.displayPreference(screen);
79         Preference preference = screen.findPreference(getPreferenceKey());
80         if (preference != null) {
81             preference.setEnabled(!isInCallState());
82         }
83     }
84 
isInCallState()85     boolean isInCallState() {
86         TelephonyManager telephonyManager = mContext.getSystemService(TelephonyManager.class);
87         return telephonyManager.getCallState(telephonyManager.getSubscriptionId())
88                 != TelephonyManager.CALL_STATE_IDLE;
89     }
90 }
91