Home | History | Annotate | Download | only in applications
      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.preference.Preference;
     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.view.ViewGroup;
     35 import android.widget.Button;
     36 import android.widget.TextView;
     37 
     38 import com.android.settings.R;
     39 import com.android.settings.Utils;
     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 View mRootView;
     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, 0);
     73     }
     74 
     75     public ClearDefaultsPreference(Context context) {
     76         this(context, null);
     77     }
     78 
     79     public void setPackageName(String packageName) {
     80         mPackageName = packageName;
     81     }
     82 
     83     public void setAppEntry(ApplicationsState.AppEntry entry) {
     84         mAppEntry = entry;
     85     }
     86 
     87     @Override
     88     protected View onCreateView(ViewGroup parent) {
     89         mRootView = super.onCreateView(parent);
     90 
     91         mActivitiesButton = (Button) mRootView.findViewById(R.id.clear_activities_button);
     92         mActivitiesButton.setOnClickListener(new View.OnClickListener() {
     93             @Override
     94             public void onClick(View v) {
     95                 if (mUsbManager != null) {
     96                     mPm.clearPackagePreferredActivities(mPackageName);
     97                     try {
     98                         mUsbManager.clearDefaults(mPackageName, UserHandle.myUserId());
     99                     } catch (RemoteException e) {
    100                         Log.e(TAG, "mUsbManager.clearDefaults", e);
    101                     }
    102                     mAppWidgetManager.setBindAppWidgetPermission(mPackageName, false);
    103                     TextView autoLaunchView = (TextView) mRootView.findViewById(R.id.auto_launch);
    104                     resetLaunchDefaultsUi(autoLaunchView);
    105                 }
    106             }
    107         });
    108 
    109         return mRootView;
    110     }
    111 
    112     @Override
    113     protected void onBindView(View view) {
    114         super.onBindView(view);
    115 
    116         updateUI();
    117     }
    118 
    119     public boolean updateUI() {
    120         boolean hasBindAppWidgetPermission =
    121                 mAppWidgetManager.hasBindAppWidgetPermission(mAppEntry.info.packageName);
    122 
    123         TextView autoLaunchView = (TextView) mRootView.findViewById(R.id.auto_launch);
    124         boolean autoLaunchEnabled = Utils.hasPreferredActivities(mPm, mPackageName)
    125                 || Utils.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 void resetLaunchDefaultsUi(TextView autoLaunchView) {
    169         autoLaunchView.setText(R.string.auto_launch_disable_text);
    170         // Disable clear activities button
    171         mActivitiesButton.setEnabled(false);
    172     }
    173 }
    174