1 /*
2  * Copyright (C) 2024 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.traceur;
18 
19 import android.content.SharedPreferences;
20 import android.graphics.drawable.Icon;
21 import android.preference.PreferenceManager;
22 import android.service.quicksettings.Tile;
23 import android.service.quicksettings.TileService;
24 import android.util.Log;
25 
26 public class StackSamplingQsService extends TileService {
27 
28     private static final String TAG = "Traceur";
29     private static StackSamplingQsService sListeningInstance;
30 
updateTile()31     public static void updateTile() {
32         if (sListeningInstance != null) {
33             sListeningInstance.update();
34         }
35     }
36 
37     @Override
onStartListening()38     public void onStartListening() {
39         sListeningInstance = this;
40         update();
41     }
42 
43     @Override
onStopListening()44     public void onStopListening() {
45         if (sListeningInstance == this) {
46             sListeningInstance = null;
47         }
48     }
49 
update()50     private void update() {
51         Receiver.updateDeveloperOptionsWatcher(this, /* fromBootIntent */ false);
52         if (getQsTile() == null) {
53             Log.w(TAG, "StackSamplingQsService.getQsTile() returned null");
54             return;
55         }
56 
57         SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
58         boolean tracingOn = prefs.getBoolean(getString(R.string.pref_key_tracing_on), false);
59         boolean stackSamplingOn = prefs.getBoolean(
60                 getString(R.string.pref_key_stack_sampling_on), false);
61         boolean heapDumpOn = prefs.getBoolean(
62                 getString(R.string.pref_key_heap_dump_on), false);
63 
64         String titleString = getString(stackSamplingOn ?
65                 R.string.stop_stack_sampling: R.string.record_stack_samples);
66 
67         getQsTile().setIcon(Icon.createWithResource(this, R.drawable.bugfood_icon));
68         // If a trace or heap dumps are being recorded, this tile is made uninteractable.
69         // Otherwise, it reflects the current stack sampling state (active vs. inactive).
70         getQsTile().setState((tracingOn || heapDumpOn) ? Tile.STATE_UNAVAILABLE :
71                 (stackSamplingOn ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE));
72         getQsTile().setLabel(titleString);
73         getQsTile().updateTile();
74     }
75 
76     /** When we click the tile, toggle stack sampling state.
77      *  If stack sampling is being turned off, dump and offer to share. */
78     @Override
onClick()79     public void onClick() {
80         SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
81         boolean newState = !prefs.getBoolean(getString(R.string.pref_key_stack_sampling_on), false);
82         prefs.edit().putBoolean(getString(R.string.pref_key_stack_sampling_on), newState).commit();
83 
84         Receiver.updateTracing(this);
85     }
86 }
87