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