• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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.Context;
20 import android.content.Intent;
21 import android.content.SharedPreferences;
22 import android.preference.PreferenceManager;
23 import android.provider.Settings;
24 import android.util.Log;
25 
26 public class StopTraceService extends TraceService {
27     private static final String TAG = "Traceur";
28 
StopTraceService()29     public StopTraceService() {
30         super("StopTraceService");
31         setIntentRedelivery(true);
32     }
33 
34     /* If we stop a trace using this entrypoint, we must also reset the preference and the
35      * Quick Settings UI, since this may be the only indication that the user wants to stop the
36      * trace.
37     */
38     @Override
onHandleIntent(Intent intent)39     public void onHandleIntent(Intent intent) {
40         Context context = getApplicationContext();
41         if (!Receiver.isTraceurAllowed(context)) {
42             return;
43         }
44         // Ensures that only intents that pertain to stopping a trace and need to be accessed from
45         // outside Traceur are passed to TraceService through StopTraceService.
46         String intentAction = intent.getAction();
47         if (!intentAction.equals(TraceService.INTENT_ACTION_NOTIFY_SESSION_STOLEN) &&
48             !intentAction.equals(TraceService.INTENT_ACTION_NOTIFY_SESSION_STOPPED)) {
49             return;
50         }
51         SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
52         boolean prefsTracingOn =
53             prefs.getBoolean(context.getString(R.string.pref_key_tracing_on), false);
54 
55         // If the user thinks tracing is off and the trace processor agrees, we have no work to do.
56         // We must still start a foreground service, but let's log as an FYI.
57         if (!prefsTracingOn && !TraceUtils.isTracingOn()) {
58             Log.i(TAG, "StopTraceService does not see a trace to stop.");
59         }
60 
61         PreferenceManager.getDefaultSharedPreferences(context)
62                 .edit().putBoolean(context.getString(R.string.pref_key_tracing_on),
63                         false).commit();
64         context.sendBroadcast(new Intent(MainFragment.ACTION_REFRESH_TAGS));
65         TraceService.updateAllQuickSettingsTiles();
66         super.onHandleIntent(intent);
67     }
68 }
69