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.intelligence;
18 
19 import android.app.Service;
20 import android.content.Intent;
21 import android.content.pm.PackageManager;
22 import android.content.pm.ResolveInfo;
23 import android.os.IBinder;
24 import androidx.annotation.VisibleForTesting;
25 
26 import com.android.settings.intelligence.suggestions.model.SuggestionCategory;
27 import com.android.settings.intelligence.suggestions.model.SuggestionCategoryRegistry;
28 
29 import java.io.FileDescriptor;
30 import java.io.PrintWriter;
31 import java.util.List;
32 
33 /**
34  * A service that reacts to adb shell dumpsys.
35  * <p/>
36  * adb shell am startservice com.android.settings.intelligence/\
37  *      .SettingsIntelligenceDumpService
38  * adb shell dumpsys activity service com.android.settings.intelligence/\
39  *      .SettingsIntelligenceDumpService
40  */
41 public class SettingsIntelligenceDumpService extends Service {
42 
43     private static final String KEY_SUGGESTION_CATEGORY = "suggestion_category: ";
44 
45     @Override
onBind(Intent intent)46     public IBinder onBind(Intent intent) {
47         return null;
48     }
49 
50     @Override
dump(FileDescriptor fd, PrintWriter writer, String[] args)51     protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
52         final StringBuilder dump = new StringBuilder();
53         dump.append(getString(R.string.app_name_settings_intelligence))
54                 .append('\n')
55                 .append(dumpSuggestions());
56         writer.println(dump.toString());
57     }
58 
59     @VisibleForTesting
dumpSuggestions()60     String dumpSuggestions() {
61         final StringBuilder dump = new StringBuilder();
62         final PackageManager pm = getPackageManager();
63         dump.append(" suggestion dump\n");
64         for (SuggestionCategory category : SuggestionCategoryRegistry.CATEGORIES) {
65             dump.append(KEY_SUGGESTION_CATEGORY)
66                     .append(category.getCategory())
67                     .append('\n');
68 
69             final Intent probe = new Intent(Intent.ACTION_MAIN);
70             probe.addCategory(category.getCategory());
71             final List<ResolveInfo> results = pm
72                     .queryIntentActivities(probe, PackageManager.GET_META_DATA);
73             if (results == null || results.isEmpty()) {
74                 continue;
75             }
76             for (ResolveInfo info : results) {
77                 dump.append("\t\t")
78                         .append(info.activityInfo.packageName)
79                         .append('/')
80                         .append(info.activityInfo.name)
81                         .append('\n');
82             }
83         }
84         return dump.toString();
85     }
86 }
87