OSDN Git Service

Merge changes I37c5df89,I453d8359
[android-x86/packages-apps-Settings.git] / src / com / android / settings / applications / appinfo / AppMemoryPreferenceController.java
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
17 package com.android.settings.applications.appinfo;
18
19 import android.app.Activity;
20 import android.app.slice.Slice;
21 import android.content.Context;
22 import android.content.pm.PackageInfo;
23 import android.os.AsyncTask;
24 import android.support.v7.preference.Preference;
25 import android.support.v7.preference.PreferenceScreen;
26 import android.text.format.Formatter;
27
28 import com.android.settings.R;
29 import com.android.settings.SettingsActivity;
30 import com.android.settings.applications.AppInfoDashboardFragment;
31 import com.android.settings.applications.ProcStatsData;
32 import com.android.settings.applications.ProcStatsEntry;
33 import com.android.settings.applications.ProcStatsPackageEntry;
34 import com.android.settings.applications.ProcessStatsBase;
35 import com.android.settings.core.BasePreferenceController;
36 import com.android.settingslib.core.lifecycle.Lifecycle;
37 import com.android.settingslib.core.lifecycle.LifecycleObserver;
38 import com.android.settingslib.core.lifecycle.events.OnResume;
39 import com.android.settingslib.development.DevelopmentSettingsEnabler;
40
41 public class AppMemoryPreferenceController extends BasePreferenceController
42         implements LifecycleObserver, OnResume {
43
44     private static final String KEY_MEMORY = "memory";
45
46     private Preference mPreference;
47     private final AppInfoDashboardFragment mParent;
48     private ProcStatsData mStatsManager;
49     private ProcStatsPackageEntry mStats;
50
51     private class MemoryUpdater extends AsyncTask<Void, Void, ProcStatsPackageEntry> {
52
53         @Override
54         protected ProcStatsPackageEntry doInBackground(Void... params) {
55             final Activity activity = mParent.getActivity();
56             if (activity == null) {
57                 return null;
58             }
59             PackageInfo packageInfo = mParent.getPackageInfo();
60             if (packageInfo == null) {
61                 return null;
62             }
63             if (mStatsManager == null) {
64                 mStatsManager = new ProcStatsData(activity, false);
65                 mStatsManager.setDuration(ProcessStatsBase.sDurations[0]);
66             }
67             mStatsManager.refreshStats(true);
68             for (ProcStatsPackageEntry pkgEntry : mStatsManager.getEntries()) {
69                 for (ProcStatsEntry entry : pkgEntry.getEntries()) {
70                     if (entry.getUid() == packageInfo.applicationInfo.uid) {
71                         pkgEntry.updateMetrics();
72                         return pkgEntry;
73                     }
74                 }
75             }
76             return null;
77         }
78
79         @Override
80         protected void onPostExecute(ProcStatsPackageEntry entry) {
81             if (mParent.getActivity() == null) {
82                 return;
83             }
84             if (entry != null) {
85                 mStats = entry;
86                 mPreference.setEnabled(true);
87                 double amount = Math.max(entry.getRunWeight(), entry.getBgWeight())
88                         * mStatsManager.getMemInfo().getWeightToRam();
89                 mPreference.setSummary(mContext.getString(R.string.memory_use_summary,
90                         Formatter.formatShortFileSize(mContext, (long) amount)));
91             } else {
92                 mPreference.setEnabled(false);
93                 mPreference.setSummary(mContext.getString(R.string.no_memory_use_summary));
94             }
95         }
96     }
97
98     public AppMemoryPreferenceController(Context context, AppInfoDashboardFragment parent,
99             Lifecycle lifecycle) {
100         super(context, KEY_MEMORY);
101         mParent = parent;
102         if (lifecycle != null) {
103             lifecycle.addObserver(this);
104         }
105     }
106
107     @Override
108     public int getAvailabilityStatus() {
109         return DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(mContext)
110                 ? AVAILABLE : DISABLED_DEPENDENT_SETTING;
111     }
112
113     @Override
114     public Slice getSettingSlice() {
115         return null;
116     }
117
118     @Override
119     public void displayPreference(PreferenceScreen screen) {
120         super.displayPreference(screen);
121         mPreference = screen.findPreference(getPreferenceKey());
122     }
123
124     @Override
125     public boolean handlePreferenceTreeClick(Preference preference) {
126         if (KEY_MEMORY.equals(preference.getKey())) {
127             ProcessStatsBase.launchMemoryDetail((SettingsActivity) mParent.getActivity(),
128                     mStatsManager.getMemInfo(), mStats, false);
129             return true;
130         }
131         return false;
132     }
133
134     @Override
135     public void onResume() {
136         if (isAvailable()) {
137             new MemoryUpdater().execute();
138         }
139     }
140
141 }