ExternalSourceDetailPreferenceController.java revision 765007b0bfb2f27707e2394b597d208f8e512bb8
1/*
2 * Copyright (C) 2017 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.appinfo;
18
19import android.content.Context;
20import android.os.UserManager;
21import android.support.annotation.VisibleForTesting;
22import android.support.v7.preference.Preference;
23
24import com.android.settings.SettingsPreferenceFragment;
25import com.android.settings.applications.AppInfoDashboardFragment;
26import com.android.settings.applications.AppStateInstallAppsBridge;
27
28public class ExternalSourceDetailPreferenceController extends AppInfoPreferenceControllerBase {
29
30    private static final String KEY = "install_other_apps";
31
32    private final String mPackageName;
33
34    public ExternalSourceDetailPreferenceController(Context context,
35            AppInfoDashboardFragment parent, String packageName) {
36        super(context, parent, KEY);
37        mPackageName = packageName;
38    }
39
40    @Override
41    public int getAvailabilityStatus() {
42        if (UserManager.get(mContext).isManagedProfile()) {
43            return DISABLED_FOR_USER;
44        }
45        return isPotentialAppSource() ? AVAILABLE : DISABLED_FOR_USER;
46    }
47
48    @Override
49    public void updateState(Preference preference) {
50        preference.setSummary(getPreferenceSummary());
51    }
52
53    @Override
54    protected Class<? extends SettingsPreferenceFragment> getDetailFragmentClass() {
55        return ExternalSourcesDetails.class;
56    }
57
58    @VisibleForTesting
59    CharSequence getPreferenceSummary() {
60        return ExternalSourcesDetails.getPreferenceSummary(mContext, mParent.getAppEntry());
61    }
62
63    @VisibleForTesting
64    boolean isPotentialAppSource() {
65        AppStateInstallAppsBridge.InstallAppsState appState =
66                new AppStateInstallAppsBridge(mContext, null, null).createInstallAppsStateFor(
67                        mPackageName, mParent.getPackageInfo().applicationInfo.uid);
68        return appState.isPotentialAppSource();
69    }
70
71}
72