summaryrefslogtreecommitdiffstats
path: root/src/com/android/settings/applications/AppLaunchSettings.java
blob: c885b0262e7bf641b0a23a36e9a5aab4716594a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settings.applications;

import android.app.AlertDialog;
import android.content.IntentFilter;
import android.content.pm.ApplicationInfo;
import android.content.pm.IntentFilterVerificationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.UserHandle;
import android.preference.Preference;
import android.preference.SwitchPreference;
import android.util.ArraySet;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;

import com.android.internal.logging.MetricsLogger;
import com.android.settings.DropDownPreference;
import com.android.settings.DropDownPreference.Callback;
import com.android.settings.R;
import com.android.settings.Utils;

import static android.content.pm.PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED;
import static android.content.pm.PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ASK;
import static android.content.pm.PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS;
import static android.content.pm.PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS_ASK;
import static android.content.pm.PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_NEVER;

import java.util.List;

public class AppLaunchSettings extends AppInfoWithHeader implements OnClickListener,
        Preference.OnPreferenceChangeListener {
    private static final String TAG = "AppLaunchSettings";

    private static final String KEY_APP_LINK_STATE = "app_link_state";
    private static final String KEY_SUPPORTED_DOMAIN_URLS = "app_launch_supported_domain_urls";
    private static final String KEY_CLEAR_DEFAULTS = "app_launch_clear_defaults";

    private PackageManager mPm;

    private boolean mHasDomainUrls;
    private DropDownPreference mAppLinkState;
    private AppDomainsPreference mAppDomainUrls;
    private ClearDefaultsPreference mClearDefaultsPreference;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.installed_app_launch_settings);

        mPm = getActivity().getPackageManager();

        mHasDomainUrls =
                (mAppEntry.info.privateFlags & ApplicationInfo.PRIVATE_FLAG_HAS_DOMAIN_URLS) != 0;
        List<IntentFilterVerificationInfo> iviList = mPm.getIntentFilterVerifications(mPackageName);

        List<IntentFilter> filters = mPm.getAllIntentFilters(mPackageName);

        mAppDomainUrls = (AppDomainsPreference) findPreference(KEY_SUPPORTED_DOMAIN_URLS);
        CharSequence[] entries = getEntries(mPackageName, iviList, filters);
        mAppDomainUrls.setTitles(entries);
        mAppDomainUrls.setValues(new int[entries.length]);

        mClearDefaultsPreference = (ClearDefaultsPreference) findPreference(KEY_CLEAR_DEFAULTS);

        buildStateDropDown();
    }

    private void buildStateDropDown() {
        mAppLinkState = (DropDownPreference) findPreference(KEY_APP_LINK_STATE);

        // Designed order of states in the dropdown:
        //
        // * always
        // * ask
        // * never
        mAppLinkState.addItem(R.string.app_link_open_always,
                INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS);
        mAppLinkState.addItem(R.string.app_link_open_ask,
                INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS_ASK);
        mAppLinkState.addItem(R.string.app_link_open_never,
                INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_NEVER);

        mAppLinkState.setEnabled(mHasDomainUrls);
        if (mHasDomainUrls) {
            // Present 'undefined' as 'ask' because the OS treats them identically for
            // purposes of the UI (and does the right thing around pending domain
            // verifications that might arrive after the user chooses 'ask' in this UI).
            final int state = mPm.getIntentVerificationStatus(mPackageName, UserHandle.myUserId());
            mAppLinkState.setSelectedValue(
                    (state == INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED)
                        ? INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS_ASK
                        : state);

            // Set the callback only after setting the initial selected item
            mAppLinkState.setCallback(new Callback() {
                @Override
                public boolean onItemSelected(int pos, Object value) {
                    return updateAppLinkState((Integer) value);
                }
            });
        }
    }

    private boolean updateAppLinkState(final int newState) {
        final int userId = UserHandle.myUserId();
        final int priorState = mPm.getIntentVerificationStatus(mPackageName, userId);

        if (priorState == newState) {
            return false;
        }

        boolean success = mPm.updateIntentVerificationStatus(mPackageName, newState, userId);
        if (success) {
            // Read back the state to see if the change worked
            final int updatedState = mPm.getIntentVerificationStatus(mPackageName, userId);
            success = (newState == updatedState);
        } else {
            Log.e(TAG, "Couldn't update intent verification status!");
        }
        return success;
    }

    private CharSequence[] getEntries(String packageName, List<IntentFilterVerificationInfo> iviList,
            List<IntentFilter> filters) {
        ArraySet<String> result = Utils.getHandledDomains(mPm, packageName);
        return result.toArray(new CharSequence[result.size()]);
    }

    @Override
    protected boolean refreshUi() {
        mClearDefaultsPreference.setPackageName(mPackageName);
        mClearDefaultsPreference.setAppEntry(mAppEntry);
        return true;
    }

    @Override
    protected AlertDialog createDialog(int id, int errorCode) {
        // No dialogs for preferred launch settings.
        return null;
    }

    @Override
    public void onClick(View v) {
        // Nothing to do
    }

    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        // actual updates are handled by the app link dropdown callback
        return true;
    }

    @Override
    protected int getMetricsCategory() {
        return MetricsLogger.APPLICATIONS_APP_LAUNCH;
    }
}