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 TracingQsService extends TileService {
27 
28     private static final String TAG = "Traceur";
29     private static TracingQsService 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, "TracingQsService.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(tracingOn ? R.string.stop_tracing: R.string.record_trace);
65 
66         getQsTile().setIcon(Icon.createWithResource(this, R.drawable.bugfood_icon));
67         // If stack samples or heap dumps are being recorded, this tile is made uninteractable.
68         // Otherwise, it reflects the current trace state (active vs. inactive).
69         getQsTile().setState((stackSamplingOn || heapDumpOn) ? Tile.STATE_UNAVAILABLE :
70                 (tracingOn ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE));
71         getQsTile().setLabel(titleString);
72         getQsTile().updateTile();
73     }
74 
75     /** When we click the tile, toggle tracing state.
76      *  If tracing is being turned off, dump and offer to share. */
77     @Override
onClick()78     public void onClick() {
79         SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
80         boolean newState = !prefs.getBoolean(getString(R.string.pref_key_tracing_on), false);
81         prefs.edit().putBoolean(getString(R.string.pref_key_tracing_on), newState).commit();
82 
83         Receiver.updateTracing(this);
84     }
85 }
86