1 /* 2 * Copyright (C) 2016 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 QsService extends TileService { 27 28 private static final String TAG = "Traceur"; 29 private static QsService 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, "QsService.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 62 String titleString = getString(tracingOn ? R.string.stop_tracing: R.string.record_trace); 63 64 getQsTile().setIcon(Icon.createWithResource(this, R.drawable.bugfood_icon)); 65 // If stack sampling is active, the "Record trace" tile cannot be interacted with. 66 // Otherwise, it should reflect the current trace state (active vs. inactive). 67 getQsTile().setState(stackSamplingOn ? Tile.STATE_UNAVAILABLE : 68 (tracingOn ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE)); 69 getQsTile().setLabel(titleString); 70 getQsTile().updateTile(); 71 } 72 73 /** When we click the tile, toggle tracing state. 74 * If tracing is being turned off, dump and offer to share. */ 75 @Override onClick()76 public void onClick() { 77 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 78 boolean newTracingState = !prefs.getBoolean(getString(R.string.pref_key_tracing_on), false); 79 prefs.edit().putBoolean(getString(R.string.pref_key_tracing_on), newTracingState).commit(); 80 81 Receiver.updateTracing(this); 82 } 83 } 84