1 /*
2  * Copyright (C) 2019 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.permissioncontroller.permission.service;
18 
19 
20 import android.content.Intent;
21 import android.permission.PermissionControllerService;
22 
23 import androidx.annotation.CallSuper;
24 import androidx.annotation.NonNull;
25 import androidx.annotation.Nullable;
26 import androidx.lifecycle.Lifecycle;
27 import androidx.lifecycle.LifecycleOwner;
28 
29 /**
30  * A slightly modified version of the AndroidX LifecycleService. The only change is there is no
31  * onBind override, since it is final in PermissionControllerService.
32  */
33 public abstract class PermissionControllerLifecycleService extends
34         PermissionControllerService implements LifecycleOwner {
35 
36     private final ServiceLifecycleDispatcher mDispatcher = new ServiceLifecycleDispatcher(this);
37 
38     @CallSuper
39     @Override
onCreate()40     public void onCreate() {
41         mDispatcher.onServicePreSuperOnCreate();
42         super.onCreate();
43     }
44 
45     @SuppressWarnings("deprecation")
46     @CallSuper
47     @Override
onStart(@ullable Intent intent, int startId)48     public void onStart(@Nullable Intent intent, int startId) {
49         mDispatcher.onServicePreSuperOnStart();
50         super.onStart(intent, startId);
51     }
52 
53     /**
54       * onBind is final in PermissionControllerService, so we have to wait for a request to set
55       * our state to "started".
56      */
setLifecycleToStarted()57     void setLifecycleToStarted() {
58         mDispatcher.onServicePreSuperOnBind();
59     }
60 
61     /**
62      * This method is added only to annotate it with @CallSuper.
63      * In usual service super.onStartCommand is no-op, but in LifecycleService
64      * it results in mDispatcher.onServicePreSuperOnBind() call, because
65      * super.onStartCommand calls onStart().
66      */
67     @CallSuper
68     @Override
onStartCommand(@ullable Intent intent, int flags, int startId)69     public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
70         return super.onStartCommand(intent, flags, startId);
71     }
72 
73     @CallSuper
74     @Override
onUnbind(@ullable Intent intent)75     public boolean onUnbind(@Nullable Intent intent) {
76         mDispatcher.onServicePreSuperOnUnbind();
77         return true;
78     }
79 
80     @CallSuper
81     @Override
onRebind(@ullable Intent intent)82     public void onRebind(@Nullable Intent intent) {
83         mDispatcher.onServicePreSuperOnBind();
84     }
85 
86     @CallSuper
87     @Override
onDestroy()88     public void onDestroy() {
89         mDispatcher.onServicePreSuperOnDestroy();
90         super.onDestroy();
91     }
92 
93     @Override
94     @NonNull
getLifecycle()95     public Lifecycle getLifecycle() {
96         return mDispatcher.getLifecycle();
97     }
98 }
99