TimeSpentInAppPreferenceController.java revision bd376296cd5b5a41c421b4d831036957915336ce
1/*
2 * Copyright (C) 2018 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.content.Intent;
21import android.content.pm.ApplicationInfo;
22import android.content.pm.PackageManager;
23import android.content.pm.ResolveInfo;
24import android.support.annotation.VisibleForTesting;
25import android.support.v7.preference.Preference;
26import android.support.v7.preference.PreferenceScreen;
27import android.text.TextUtils;
28
29import com.android.settings.core.BasePreferenceController;
30
31import java.util.List;
32
33public class TimeSpentInAppPreferenceController extends BasePreferenceController {
34
35    @VisibleForTesting
36    static final Intent SEE_TIME_IN_APP_TEMPLATE =
37            new Intent("com.android.settings.action.TIME_SPENT_IN_APP");
38
39    private final PackageManager mPackageManager;
40
41    private Intent mIntent;
42    private String mPackageName;
43
44    public TimeSpentInAppPreferenceController(Context context, String preferenceKey) {
45        super(context, preferenceKey);
46        mPackageManager = context.getPackageManager();
47    }
48
49    public void setPackageName(String packageName) {
50        mPackageName = packageName;
51        mIntent = new Intent(SEE_TIME_IN_APP_TEMPLATE)
52                .putExtra(Intent.EXTRA_PACKAGE_NAME, mPackageName);
53    }
54
55    @Override
56    public int getAvailabilityStatus() {
57        if (TextUtils.isEmpty(mPackageName)) {
58            return UNSUPPORTED_ON_DEVICE;
59        }
60        final List<ResolveInfo> resolved = mPackageManager.queryIntentActivities(mIntent,
61                0 /* flags */);
62        if (resolved == null || resolved.isEmpty()) {
63            return UNSUPPORTED_ON_DEVICE;
64        }
65        for (ResolveInfo info : resolved) {
66            if (isSystemApp(info)) {
67                return AVAILABLE;
68            }
69        }
70        return UNSUPPORTED_ON_DEVICE;
71    }
72
73    @Override
74    public void displayPreference(PreferenceScreen screen) {
75        super.displayPreference(screen);
76        final Preference pref = screen.findPreference(getPreferenceKey());
77        if (pref != null) {
78            pref.setIntent(mIntent);
79        }
80    }
81
82    private boolean isSystemApp(ResolveInfo info) {
83        return info != null
84                && info.activityInfo != null
85                && info.activityInfo.applicationInfo != null
86                && (info.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
87    }
88}
89