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