blob: cb9523fcaed150300caa8a85568f65fae591b2a5 [file] [log] [blame]
Dianne Hackborn83b40f62017-04-26 13:59:47 -07001/*
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
17package com.android.systemui;
18
19import android.annotation.NonNull;
20import android.annotation.Nullable;
21import android.content.Context;
22import android.content.DialogInterface;
23import android.content.Intent;
24import android.content.pm.ApplicationInfo;
25import android.content.pm.PackageManager;
26import android.net.Uri;
27import android.os.Bundle;
28import android.provider.Settings;
Dianne Hackborn157a3f92017-05-01 12:33:31 -070029import android.util.IconDrawableFactory;
Dianne Hackborn83b40f62017-04-26 13:59:47 -070030import android.util.Log;
31import android.view.LayoutInflater;
32import android.view.View;
33import android.view.ViewGroup;
34import android.widget.AdapterView;
35import android.widget.ArrayAdapter;
36import android.widget.ImageView;
37import android.widget.ListView;
38import android.widget.TextView;
39
40import com.android.internal.app.AlertActivity;
41import com.android.internal.app.AlertController;
42import com.android.internal.logging.MetricsLogger;
43import com.android.internal.logging.nano.MetricsProto;
44
Dianne Hackborn83b40f62017-04-26 13:59:47 -070045import java.util.ArrayList;
46
47/**
48 * Show a list of currently running foreground services (supplied by the caller)
49 * that the user can tap through to their application details.
50 */
51public final class ForegroundServicesDialog extends AlertActivity implements
52 AdapterView.OnItemSelectedListener, DialogInterface.OnClickListener,
53 AlertController.AlertParams.OnPrepareListViewListener {
54
55 private static final String TAG = "ForegroundServicesDialog";
56
57 LayoutInflater mInflater;
58
59 private MetricsLogger mMetricsLogger;
60
61 private String[] mPackages;
62 private PackageItemAdapter mAdapter;
63
64 private DialogInterface.OnClickListener mAppClickListener =
65 new DialogInterface.OnClickListener() {
66 public void onClick(DialogInterface dialog, int which) {
Dianne Hackborn4ae51e02017-05-04 16:53:11 -070067 String pkg = mAdapter.getItem(which).packageName;
Dianne Hackborn83b40f62017-04-26 13:59:47 -070068 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
69 intent.setData(Uri.fromParts("package", pkg, null));
70 startActivity(intent);
71 finish();
72 }
73 };
74
75 @Override
76 protected void onCreate(Bundle savedInstanceState) {
77 super.onCreate(savedInstanceState);
78 Dependency.initDependencies(getApplicationContext());
79
80 mMetricsLogger = Dependency.get(MetricsLogger.class);
81
82 mInflater = LayoutInflater.from(this);
83
84 mAdapter = new PackageItemAdapter(this);
85
86 final AlertController.AlertParams p = mAlertParams;
87 p.mAdapter = mAdapter;
88 p.mOnClickListener = mAppClickListener;
89 p.mCustomTitleView = mInflater.inflate(R.layout.foreground_service_title, null);
90 p.mIsSingleChoice = true;
91 p.mOnItemSelectedListener = this;
92 p.mPositiveButtonText = getString(com.android.internal.R.string.done_label);
93 p.mPositiveButtonListener = this;
94 p.mOnPrepareListViewListener = this;
95
96 updateApps(getIntent());
97 if (mPackages == null) {
98 Log.w(TAG, "No packages supplied");
99 finish();
100 return;
101 }
102
103 setupAlert();
104 }
105
106 @Override
107 protected void onResume() {
108 super.onResume();
109 mMetricsLogger.visible(MetricsProto.MetricsEvent.RUNNING_BACKGROUND_APPS_DIALOG);
110 }
111
112 @Override
113 protected void onPause() {
114 super.onPause();
115 mMetricsLogger.hidden(MetricsProto.MetricsEvent.RUNNING_BACKGROUND_APPS_DIALOG);
116 }
117
118 @Override
119 protected void onNewIntent(Intent intent) {
120 super.onNewIntent(intent);
121 updateApps(intent);
122 }
123
124 @Override
125 protected void onStop() {
126 super.onStop();
127
128 // This is a transient dialog, if the user leaves it then it goes away,
129 // they can return back to it from the notification.
130 if (!isChangingConfigurations()) {
131 finish();
132 }
133 }
134
135 void updateApps(Intent intent) {
136 mPackages = intent.getStringArrayExtra("packages");
137 if (mPackages != null) {
138 mAdapter.setPackages(mPackages);
139 }
140 }
141
142 public void onPrepareListView(ListView listView) {
143 }
144
145 /*
146 * On click of Ok/Cancel buttons
147 */
148 public void onClick(DialogInterface dialog, int which) {
149 finish();
150 }
151
152 public void onItemSelected(AdapterView parent, View view, int position, long id) {
153 }
154
155 public void onNothingSelected(AdapterView parent) {
156 }
157
158 static private class PackageItemAdapter extends ArrayAdapter<ApplicationInfo> {
159 final PackageManager mPm;
160 final LayoutInflater mInflater;
Dianne Hackborn157a3f92017-05-01 12:33:31 -0700161 final IconDrawableFactory mIconDrawableFactory;
Dianne Hackborn83b40f62017-04-26 13:59:47 -0700162
163 public PackageItemAdapter(Context context) {
164 super(context, R.layout.foreground_service_item);
165 mPm = context.getPackageManager();
166 mInflater = LayoutInflater.from(context);
Dianne Hackborn157a3f92017-05-01 12:33:31 -0700167 mIconDrawableFactory = IconDrawableFactory.newInstance(context, true);
Dianne Hackborn83b40f62017-04-26 13:59:47 -0700168 }
169
170 public void setPackages(String[] packages) {
171 clear();
172
173 ArrayList<ApplicationInfo> apps = new ArrayList<>();
174 for (int i = 0; i < packages.length; i++) {
175 try {
176 apps.add(mPm.getApplicationInfo(packages[i],
177 PackageManager.MATCH_KNOWN_PACKAGES));
178 } catch (PackageManager.NameNotFoundException e) {
179 }
180 }
181
182 apps.sort(new ApplicationInfo.DisplayNameComparator(mPm));
183 addAll(apps);
184 }
185
186 public @NonNull
187 View getView(int position, @Nullable View convertView,
188 @NonNull ViewGroup parent) {
189 final View view;
190 if (convertView == null) {
191 view = mInflater.inflate(R.layout.foreground_service_item, parent, false);
192 } else {
193 view = convertView;
194 }
195
196 ImageView icon = view.findViewById(R.id.app_icon);
Dianne Hackborn157a3f92017-05-01 12:33:31 -0700197 icon.setImageDrawable(mIconDrawableFactory.getBadgedIcon(getItem(position)));
Dianne Hackborn83b40f62017-04-26 13:59:47 -0700198
199 TextView label = view.findViewById(R.id.app_name);
200 label.setText(getItem(position).loadLabel(mPm));
201
202 return view;
203 }
204 }
205}