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 25 public class QsService extends TileService { 26 27 private static QsService sListeningInstance; 28 updateTile()29 public static void updateTile() { 30 if (sListeningInstance != null) { 31 sListeningInstance.update(); 32 } 33 } 34 35 @Override onStartListening()36 public void onStartListening() { 37 sListeningInstance = this; 38 update(); 39 } 40 41 @Override onStopListening()42 public void onStopListening() { 43 if (sListeningInstance == this) { 44 sListeningInstance = null; 45 } 46 } 47 update()48 private void update() { 49 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 50 boolean tracingOn = prefs.getBoolean(getString(R.string.pref_key_tracing_on), false); 51 52 String titleString = getString(tracingOn ? R.string.stop_tracing: R.string.record_trace); 53 54 getQsTile().setIcon(Icon.createWithResource(this, R.drawable.bugfood_icon)); 55 getQsTile().setState(tracingOn ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE); 56 getQsTile().setLabel(titleString); 57 getQsTile().updateTile(); 58 Receiver.updateDeveloperOptionsWatcher(this); 59 } 60 61 /** When we click the tile, toggle tracing state. 62 * If tracing is being turned off, dump and offer to share. */ 63 @Override onClick()64 public void onClick() { 65 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 66 boolean newTracingState = !prefs.getBoolean(getString(R.string.pref_key_tracing_on), false); 67 prefs.edit().putBoolean(getString(R.string.pref_key_tracing_on), newTracingState).commit(); 68 69 Receiver.updateTracing(this); 70 } 71 } 72