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