RunningServices.java revision 5a638a6ad159dd489de3da1089327f71e2138911
1/*
2 * Copyright (C) 2015 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 */
16package com.android.settings.applications;
17
18import android.app.Fragment;
19import android.os.Bundle;
20import android.view.LayoutInflater;
21import android.view.Menu;
22import android.view.MenuInflater;
23import android.view.MenuItem;
24import android.view.View;
25import android.view.ViewGroup;
26
27import com.android.internal.logging.MetricsProto.MetricsEvent;
28import com.android.settings.R;
29import com.android.settings.SettingsPreferenceFragment;
30import com.android.settings.Utils;
31
32public class RunningServices extends SettingsPreferenceFragment {
33
34    private static final int SHOW_RUNNING_SERVICES = 1;
35    private static final int SHOW_BACKGROUND_PROCESSES = 2;
36
37    private RunningProcessesView mRunningProcessesView;
38    private Menu mOptionsMenu;
39    private View mLoadingContainer;
40
41    @Override
42    public void onCreate(Bundle savedInstanceState) {
43        super.onCreate(savedInstanceState);
44
45        setHasOptionsMenu(true);
46    }
47
48    @Override
49    public View onCreateView(LayoutInflater inflater, ViewGroup container,
50                Bundle savedInstanceState) {
51        View rootView = inflater.inflate(R.layout.manage_applications_running, null);
52        mRunningProcessesView = (RunningProcessesView) rootView.findViewById(
53                R.id.running_processes);
54        mRunningProcessesView.doCreate();
55        mLoadingContainer = rootView.findViewById(R.id.loading_container);
56
57        return rootView;
58    }
59
60    @Override
61    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
62        mOptionsMenu = menu;
63        menu.add(0, SHOW_RUNNING_SERVICES, 1, R.string.show_running_services)
64                .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
65        menu.add(0, SHOW_BACKGROUND_PROCESSES, 2, R.string.show_background_processes)
66                .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
67        updateOptionsMenu();
68    }
69
70    @Override
71    public void onResume() {
72        super.onResume();
73        boolean haveData = mRunningProcessesView.doResume(this, mRunningProcessesAvail);
74        Utils.handleLoadingContainer(mLoadingContainer, mRunningProcessesView, haveData, false);
75    }
76
77    @Override
78    public void onPause() {
79        super.onPause();
80        mRunningProcessesView.doPause();
81    }
82
83    @Override
84    public boolean onOptionsItemSelected(MenuItem item) {
85        switch (item.getItemId()) {
86            case SHOW_RUNNING_SERVICES:
87                mRunningProcessesView.mAdapter.setShowBackground(false);
88                break;
89            case SHOW_BACKGROUND_PROCESSES:
90                mRunningProcessesView.mAdapter.setShowBackground(true);
91                break;
92            default:
93                return false;
94        }
95        updateOptionsMenu();
96        return true;
97    }
98
99    @Override
100    public void onPrepareOptionsMenu(Menu menu) {
101        updateOptionsMenu();
102    }
103
104    private void updateOptionsMenu() {
105        boolean showingBackground = mRunningProcessesView.mAdapter.getShowBackground();
106        mOptionsMenu.findItem(SHOW_RUNNING_SERVICES).setVisible(showingBackground);
107        mOptionsMenu.findItem(SHOW_BACKGROUND_PROCESSES).setVisible(!showingBackground);
108    }
109
110    @Override
111    protected int getMetricsCategory() {
112        return MetricsEvent.RUNNING_SERVICES;
113    }
114
115    private final Runnable mRunningProcessesAvail = new Runnable() {
116        @Override
117        public void run() {
118            Utils.handleLoadingContainer(mLoadingContainer, mRunningProcessesView, true, true);
119        }
120    };
121
122}
123