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