AppLaunchSettings.java revision 588a0881c13d8ed63ba67b3145254c22211a2019
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 17package com.android.settings.applications; 18 19import android.app.AlertDialog; 20import android.appwidget.AppWidgetManager; 21import android.content.ComponentName; 22import android.content.Context; 23import android.content.IntentFilter; 24import android.content.pm.PackageManager; 25import android.hardware.usb.IUsbManager; 26import android.os.Bundle; 27import android.os.RemoteException; 28import android.os.UserHandle; 29import android.text.SpannableString; 30import android.text.TextUtils; 31import android.text.style.BulletSpan; 32import android.util.Log; 33import android.view.LayoutInflater; 34import android.view.View; 35import android.view.View.OnClickListener; 36import android.view.ViewGroup; 37import android.widget.Button; 38import android.widget.TextView; 39 40import com.android.settings.R; 41import com.android.settings.Utils; 42import com.android.settings.applications.ApplicationsState.AppEntry; 43 44import java.util.ArrayList; 45import java.util.Collections; 46import java.util.List; 47 48public class AppLaunchSettings extends AppInfoWithHeader implements OnClickListener { 49 50 private Button mActivitiesButton; 51 private AppWidgetManager mAppWidgetManager; 52 53 private View mRootView; 54 55 @Override 56 public void onCreate(Bundle savedInstanceState) { 57 super.onCreate(savedInstanceState); 58 mAppWidgetManager = AppWidgetManager.getInstance(getActivity()); 59 } 60 61 @Override 62 public View onCreateView(LayoutInflater inflater, ViewGroup container, 63 Bundle savedInstanceState) { 64 final View view = inflater.inflate(R.layout.app_preferred_settings, container, false); 65 66 final ViewGroup allDetails = (ViewGroup) view.findViewById(R.id.all_details); 67 Utils.forceCustomPadding(allDetails, true /* additive padding */); 68 69 mActivitiesButton = (Button) view.findViewById(R.id.clear_activities_button); 70 mRootView = view; 71 72 return view; 73 } 74 75 @Override 76 protected boolean refreshUi() { 77 retrieveAppEntry(); 78 boolean hasBindAppWidgetPermission = 79 mAppWidgetManager.hasBindAppWidgetPermission(mAppEntry.info.packageName); 80 81 TextView autoLaunchTitleView = (TextView) mRootView.findViewById(R.id.auto_launch_title); 82 TextView autoLaunchView = (TextView) mRootView.findViewById(R.id.auto_launch); 83 boolean autoLaunchEnabled = hasPreferredActivities(mPm, mPackageName) 84 || hasUsbDefaults(mUsbManager, mPackageName); 85 if (!autoLaunchEnabled && !hasBindAppWidgetPermission) { 86 resetLaunchDefaultsUi(autoLaunchTitleView, autoLaunchView); 87 } else { 88 boolean useBullets = hasBindAppWidgetPermission && autoLaunchEnabled; 89 90 if (hasBindAppWidgetPermission) { 91 autoLaunchTitleView.setText(R.string.auto_launch_label_generic); 92 } else { 93 autoLaunchTitleView.setText(R.string.auto_launch_label); 94 } 95 96 CharSequence text = null; 97 int bulletIndent = getResources() 98 .getDimensionPixelSize(R.dimen.installed_app_details_bullet_offset); 99 if (autoLaunchEnabled) { 100 CharSequence autoLaunchEnableText = getText(R.string.auto_launch_enable_text); 101 SpannableString s = new SpannableString(autoLaunchEnableText); 102 if (useBullets) { 103 s.setSpan(new BulletSpan(bulletIndent), 0, autoLaunchEnableText.length(), 0); 104 } 105 text = (text == null) ? 106 TextUtils.concat(s, "\n") : TextUtils.concat(text, "\n", s, "\n"); 107 } 108 if (hasBindAppWidgetPermission) { 109 CharSequence alwaysAllowBindAppWidgetsText = 110 getText(R.string.always_allow_bind_appwidgets_text); 111 SpannableString s = new SpannableString(alwaysAllowBindAppWidgetsText); 112 if (useBullets) { 113 s.setSpan(new BulletSpan(bulletIndent), 114 0, alwaysAllowBindAppWidgetsText.length(), 0); 115 } 116 text = (text == null) ? 117 TextUtils.concat(s, "\n") : TextUtils.concat(text, "\n", s, "\n"); 118 } 119 autoLaunchView.setText(text); 120 mActivitiesButton.setEnabled(true); 121 mActivitiesButton.setOnClickListener(this); 122 } 123 return true; 124 } 125 126 private void resetLaunchDefaultsUi(TextView title, TextView autoLaunchView) { 127 title.setText(R.string.auto_launch_label); 128 autoLaunchView.setText(R.string.auto_launch_disable_text); 129 // Disable clear activities button 130 mActivitiesButton.setEnabled(false); 131 } 132 133 @Override 134 protected AlertDialog createDialog(int id, int errorCode) { 135 // No dialogs for preferred launch settings. 136 return null; 137 } 138 139 @Override 140 public void onClick(View v) { 141 if (v == mActivitiesButton) { 142 if (mUsbManager != null) { 143 mPm.clearPackagePreferredActivities(mPackageName); 144 try { 145 mUsbManager.clearDefaults(mPackageName, UserHandle.myUserId()); 146 } catch (RemoteException e) { 147 Log.e(TAG, "mUsbManager.clearDefaults", e); 148 } 149 mAppWidgetManager.setBindAppWidgetPermission(mPackageName, false); 150 TextView autoLaunchTitleView = 151 (TextView) mRootView.findViewById(R.id.auto_launch_title); 152 TextView autoLaunchView = (TextView) mRootView.findViewById(R.id.auto_launch); 153 resetLaunchDefaultsUi(autoLaunchTitleView, autoLaunchView); 154 } 155 } 156 } 157 158 private static boolean hasUsbDefaults(IUsbManager usbManager, String packageName) { 159 try { 160 if (usbManager != null) { 161 return usbManager.hasDefaults(packageName, UserHandle.myUserId()); 162 } 163 } catch (RemoteException e) { 164 Log.e(TAG, "mUsbManager.hasDefaults", e); 165 } 166 return false; 167 } 168 169 private static boolean hasPreferredActivities(PackageManager pm, String packageName) { 170 // Get list of preferred activities 171 List<ComponentName> prefActList = Collections.emptyList(); 172 // Intent list cannot be null. so pass empty list 173 List<IntentFilter> intentList = Collections.emptyList(); 174 pm.getPreferredActivities(intentList, prefActList, packageName); 175 if (localLOGV) { 176 Log.i(TAG, "Have " + prefActList.size() + " number of activities in preferred list"); 177 } 178 return prefActList.size() > 0; 179 } 180 181 public static CharSequence getSummary(AppEntry appEntry, IUsbManager usbManager, 182 PackageManager pm, Context context) { 183 String packageName = appEntry.info.packageName; 184 boolean hasPreferred = hasPreferredActivities(pm, packageName) 185 || hasUsbDefaults(usbManager, packageName); 186 return context.getString(hasPreferred 187 ? R.string.launch_defaults_some 188 : R.string.launch_defaults_none); 189 } 190 191} 192