ClearDefaultsPreference.java revision df828405b107fd6a2602580cf7e8f43723c69d58
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.appwidget.AppWidgetManager;
20import android.content.Context;
21import android.content.pm.PackageManager;
22import android.hardware.usb.IUsbManager;
23import android.os.IBinder;
24import android.os.RemoteException;
25import android.os.ServiceManager;
26import android.os.UserHandle;
27import android.support.v7.preference.Preference;
28import android.support.v7.preference.PreferenceViewHolder;
29import android.text.SpannableString;
30import android.text.TextUtils;
31import android.text.style.BulletSpan;
32import android.util.AttributeSet;
33import android.util.Log;
34import android.view.View;
35import android.widget.Button;
36import android.widget.TextView;
37
38import com.android.settings.R;
39import com.android.settingslib.applications.AppUtils;
40import com.android.settingslib.applications.ApplicationsState;
41
42public 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