Home | History | Annotate | Download | only in car
      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 import java.util.concurrent.CopyOnWriteArrayList;
     24 import java.util.function.BiConsumer;
     25 
     26 /**
     27  * This class allows one to register actions they want executed when the vehicle is being shutdown
     28  * or rebooted.
     29  *
     30  * To use this class instantiate it as part of your long-lived service, and then add actions to it.
     31  * Actions receive the Context and Intent that go with the shutdown/reboot action, which allows the
     32  * action to differentiate the two cases, should it need to do so.
     33  *
     34  * The actions will run on the UI thread.
     35  */
     36 class OnShutdownReboot {
     37     private final Object mLock = new Object();
     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 
     53     OnShutdownReboot(Context context) {
     54         mContext = context;
     55         IntentFilter shutdownFilter = new IntentFilter(Intent.ACTION_SHUTDOWN);
     56         IntentFilter rebootFilter = new IntentFilter(Intent.ACTION_REBOOT);
     57         mContext.registerReceiver(mReceiver, shutdownFilter);
     58         mContext.registerReceiver(mReceiver, rebootFilter);
     59     }
     60 
     61     OnShutdownReboot addAction(BiConsumer<Context, Intent> action) {
     62         mActions.add(action);
     63         return this;
     64     }
     65 
     66     void clearActions() {
     67         mActions.clear();
     68     }
     69 }
     70