• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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;
18 
19 import android.appwidget.AppWidgetManager;
20 import android.content.Context;
21 import android.content.pm.PackageManager;
22 import android.hardware.usb.IUsbManager;
23 import android.os.IBinder;
24 import android.os.RemoteException;
25 import android.os.ServiceManager;
26 import android.os.UserHandle;
27 import android.text.SpannableString;
28 import android.text.TextUtils;
29 import android.text.style.BulletSpan;
30 import android.util.AttributeSet;
31 import android.util.Log;
32 import android.view.View;
33 import android.widget.Button;
34 import android.widget.TextView;
35 
36 import androidx.core.content.res.TypedArrayUtils;
37 import androidx.preference.Preference;
38 import androidx.preference.PreferenceViewHolder;
39 
40 import com.android.settings.R;
41 import com.android.settingslib.applications.AppUtils;
42 import com.android.settingslib.applications.ApplicationsState;
43 
44 public class ClearDefaultsPreference extends Preference {
45 
46     protected static final String TAG = ClearDefaultsPreference.class.getSimpleName();
47 
48     private Button mActivitiesButton;
49 
50     private AppWidgetManager mAppWidgetManager;
51     private IUsbManager mUsbManager;
52     private PackageManager mPm;
53     private String mPackageName;
54     protected ApplicationsState.AppEntry mAppEntry;
55 
ClearDefaultsPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)56     public ClearDefaultsPreference(Context context, AttributeSet attrs, int defStyleAttr,
57             int defStyleRes) {
58         super(context, attrs, defStyleAttr, defStyleRes);
59 
60         setLayoutResource(R.layout.app_preferred_settings);
61 
62         mAppWidgetManager = AppWidgetManager.getInstance(context);
63         mPm = context.getPackageManager();
64         IBinder b = ServiceManager.getService(Context.USB_SERVICE);
65         mUsbManager = IUsbManager.Stub.asInterface(b);
66     }
67 
ClearDefaultsPreference(Context context, AttributeSet attrs, int defStyleAttr)68     public ClearDefaultsPreference(Context context, AttributeSet attrs, int defStyleAttr) {
69         this(context, attrs, defStyleAttr, 0);
70     }
71 
ClearDefaultsPreference(Context context, AttributeSet attrs)72     public ClearDefaultsPreference(Context context, AttributeSet attrs) {
73         this(context, attrs, TypedArrayUtils.getAttr(context,
74                 androidx.preference.R.attr.preferenceStyle,
75                 android.R.attr.preferenceStyle));
76     }
77 
ClearDefaultsPreference(Context context)78     public ClearDefaultsPreference(Context context) {
79         this(context, null);
80     }
81 
setPackageName(String packageName)82     public void setPackageName(String packageName) {
83         mPackageName = packageName;
84     }
85 
setAppEntry(ApplicationsState.AppEntry entry)86     public void setAppEntry(ApplicationsState.AppEntry entry) {
87         mAppEntry = entry;
88     }
89 
90     @Override
onBindViewHolder(final PreferenceViewHolder view)91     public void onBindViewHolder(final PreferenceViewHolder view) {
92         super.onBindViewHolder(view);
93 
94         mActivitiesButton = (Button) view.findViewById(R.id.clear_activities_button);
95         mActivitiesButton.setOnClickListener(new View.OnClickListener() {
96             @Override
97             public void onClick(View v) {
98                 if (mUsbManager != null) {
99                     final int userId = UserHandle.myUserId();
100                     mPm.clearPackagePreferredActivities(mPackageName);
101                     if (isDefaultBrowser(mPackageName)) {
102                         mPm.setDefaultBrowserPackageNameAsUser(null, userId);
103                     }
104                     try {
105                         mUsbManager.clearDefaults(mPackageName, userId);
106                     } catch (RemoteException e) {
107                         Log.e(TAG, "mUsbManager.clearDefaults", e);
108                     }
109                     mAppWidgetManager.setBindAppWidgetPermission(mPackageName, false);
110                     TextView autoLaunchView = (TextView) view.findViewById(R.id.auto_launch);
111                     resetLaunchDefaultsUi(autoLaunchView);
112                 }
113             }
114         });
115 
116         updateUI(view);
117     }
118 
updateUI(PreferenceViewHolder view)119     public boolean updateUI(PreferenceViewHolder view) {
120         boolean hasBindAppWidgetPermission =
121                 mAppWidgetManager.hasBindAppWidgetPermission(mAppEntry.info.packageName);
122 
123         TextView autoLaunchView = (TextView) view.findViewById(R.id.auto_launch);
124         boolean autoLaunchEnabled = AppUtils.hasPreferredActivities(mPm, mPackageName)
125                 || isDefaultBrowser(mPackageName)
126                 || AppUtils.hasUsbDefaults(mUsbManager, mPackageName);
127         if (!autoLaunchEnabled && !hasBindAppWidgetPermission) {
128             resetLaunchDefaultsUi(autoLaunchView);
129         } else {
130             boolean useBullets = hasBindAppWidgetPermission && autoLaunchEnabled;
131 
132             if (hasBindAppWidgetPermission) {
133                 autoLaunchView.setText(R.string.auto_launch_label_generic);
134             } else {
135                 autoLaunchView.setText(R.string.auto_launch_label);
136             }
137 
138             Context context = getContext();
139             CharSequence text = null;
140             int bulletIndent = context.getResources().getDimensionPixelSize(
141                     R.dimen.installed_app_details_bullet_offset);
142             if (autoLaunchEnabled) {
143                 CharSequence autoLaunchEnableText = context.getText(
144                         R.string.auto_launch_enable_text);
145                 SpannableString s = new SpannableString(autoLaunchEnableText);
146                 if (useBullets) {
147                     s.setSpan(new BulletSpan(bulletIndent), 0, autoLaunchEnableText.length(), 0);
148                 }
149                 text = (text == null) ?
150                         TextUtils.concat(s, "\n") : TextUtils.concat(text, "\n", s, "\n");
151             }
152             if (hasBindAppWidgetPermission) {
153                 CharSequence alwaysAllowBindAppWidgetsText =
154                         context.getText(R.string.always_allow_bind_appwidgets_text);
155                 SpannableString s = new SpannableString(alwaysAllowBindAppWidgetsText);
156                 if (useBullets) {
157                     s.setSpan(new BulletSpan(bulletIndent),
158                             0, alwaysAllowBindAppWidgetsText.length(), 0);
159                 }
160                 text = (text == null) ?
161                         TextUtils.concat(s, "\n") : TextUtils.concat(text, "\n", s, "\n");
162             }
163             autoLaunchView.setText(text);
164             mActivitiesButton.setEnabled(true);
165         }
166         return true;
167     }
168 
isDefaultBrowser(String packageName)169     private boolean isDefaultBrowser(String packageName) {
170         final String defaultBrowser = mPm.getDefaultBrowserPackageNameAsUser(UserHandle.myUserId());
171         return packageName.equals(defaultBrowser);
172     }
173 
resetLaunchDefaultsUi(TextView autoLaunchView)174     private void resetLaunchDefaultsUi(TextView autoLaunchView) {
175         autoLaunchView.setText(R.string.auto_launch_disable_text);
176         // Disable clear activities button
177         mActivitiesButton.setEnabled(false);
178     }
179 }
180