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