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