• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.car;
18 
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.IntentFilter;
23 
24 import java.util.concurrent.CopyOnWriteArrayList;
25 import java.util.function.BiConsumer;
26 
27 /**
28  * This class allows one to register actions they want executed when the vehicle is being shutdown
29  * or rebooted.
30  *
31  * To use this class instantiate it as part of your long-lived service, and then add actions to it.
32  * Actions receive the Context and Intent that go with the shutdown/reboot action, which allows the
33  * action to differentiate the two cases, should it need to do so.
34  *
35  * The actions will run on the UI thread.
36  */
37 public class OnShutdownReboot {
38 
39     private final Context mContext;
40 
41     private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
42         @Override
43         public void onReceive(Context context, Intent intent) {
44             for (BiConsumer<Context, Intent> action : mActions) {
45                 action.accept(context, intent);
46             }
47         }
48     };
49 
50     private final CopyOnWriteArrayList<BiConsumer<Context, Intent>> mActions =
51             new CopyOnWriteArrayList<>();
52 
OnShutdownReboot(Context context)53     public OnShutdownReboot(Context context) {
54         mContext = context;
55     }
56 
57     /**
58      * Initializes all resources and registers the broadcast receiver
59      */
init()60     public void init() {
61         IntentFilter filter = new IntentFilter();
62         filter.addAction(Intent.ACTION_SHUTDOWN);
63         filter.addAction(Intent.ACTION_REBOOT);
64         mContext.registerReceiver(mReceiver, filter, Context.RECEIVER_NOT_EXPORTED);
65     }
66 
67     /** Adds an action to be done at reboot/shutdown. */
addAction(BiConsumer<Context, Intent> action)68     public OnShutdownReboot addAction(BiConsumer<Context, Intent> action) {
69         mActions.add(action);
70         return this;
71     }
72 
73     /** Clears all actions. */
clearActions()74     public void clearActions() {
75         mActions.clear();
76     }
77 
78     /** Releases all resources and unregisters the broadcast receiver. */
release()79     public void release() {
80         clearActions();
81         mContext.unregisterReceiver(mReceiver);
82     }
83 }
84