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