OSDN Git Service

Refactor4Green - added comment
[android-x86/packages-apps-Taskbar.git] / app / src / playstore / java / com / farmerbb / taskbar / receiver / TaskerConditionReceiver.java
1 /* Copyright 2018 Braden Farmer
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 package com.farmerbb.taskbar.receiver;
17
18 import android.content.BroadcastReceiver;
19 import android.content.Context;
20 import android.content.Intent;
21 import android.os.Bundle;
22
23 import com.farmerbb.taskbar.service.NotificationService;
24 import com.farmerbb.taskbar.util.BundleScrubber;
25 import com.farmerbb.taskbar.util.PluginBundleManager;
26 import com.farmerbb.taskbar.util.U;
27
28 public final class TaskerConditionReceiver extends BroadcastReceiver {
29     private Bundle lastbundle = null;
30
31     @Override
32     public void onReceive(Context context, Intent intent) {
33         if(U.isExternalAccessDisabled(context)) return;
34
35         if (lastbundle.equals(intent.getBundleExtra(com.twofortyfouram.locale.api.Intent.EXTRA_BUNDLE))) {
36             // bundle hasn't changed: we can safely return
37             return;
38         }
39         updateValues(intent);
40
41         BundleScrubber.scrub(intent);
42
43         BundleScrubber.scrub(lastbundle);
44
45         if(PluginBundleManager.isBundleValid(lastbundle)) {
46             String action = lastbundle.getString(PluginBundleManager.BUNDLE_EXTRA_STRING_MESSAGE);
47
48             if(action != null) switch(action) {
49                 case "tasker_on":
50                     if(U.isServiceRunning(context, NotificationService.class))
51                         setResultCode(com.twofortyfouram.locale.api.Intent.RESULT_CONDITION_SATISFIED);
52                     else
53                         setResultCode(com.twofortyfouram.locale.api.Intent.RESULT_CONDITION_UNSATISFIED);
54                     break;
55                 case "tasker_off":
56                     if(U.isServiceRunning(context, NotificationService.class))
57                         setResultCode(com.twofortyfouram.locale.api.Intent.RESULT_CONDITION_UNSATISFIED);
58                     else
59                         setResultCode(com.twofortyfouram.locale.api.Intent.RESULT_CONDITION_SATISFIED);
60                     break;
61             }
62         }
63     }
64
65     private void updateValues(Intent intent) {
66         lastbundle = intent.getBundleExtra(com.twofortyfouram.locale.api.Intent.EXTRA_BUNDLE);
67     }
68 }