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.settingslib.development;
18 
19 import android.os.AsyncTask;
20 import android.os.IBinder;
21 import android.os.Parcel;
22 import android.os.RemoteException;
23 import android.os.ServiceManager;
24 import android.util.Log;
25 
26 import androidx.annotation.NonNull;
27 import androidx.annotation.VisibleForTesting;
28 
29 public class SystemPropPoker  {
30     private static final String TAG = "SystemPropPoker";
31 
32     private static final SystemPropPoker sInstance = new SystemPropPoker();
33 
34     private boolean mBlockPokes = false;
35 
SystemPropPoker()36     private SystemPropPoker() {}
37 
38     @NonNull
getInstance()39     public static SystemPropPoker getInstance() {
40         return sInstance;
41     }
42 
blockPokes()43     public void blockPokes() {
44         mBlockPokes = true;
45     }
46 
unblockPokes()47     public void unblockPokes() {
48         mBlockPokes = false;
49     }
50 
poke()51     public void poke() {
52         if (!mBlockPokes) {
53             createPokerTask().execute();
54         }
55     }
56 
57     @VisibleForTesting
createPokerTask()58     PokerTask createPokerTask() {
59         return new PokerTask();
60     }
61 
62     public static class PokerTask extends AsyncTask<Void, Void, Void> {
63 
64         @VisibleForTesting
listServices()65         String[] listServices() {
66             return ServiceManager.listServices();
67         }
68 
69         @VisibleForTesting
checkService(String service)70         IBinder checkService(String service) {
71             return ServiceManager.checkService(service);
72         }
73 
74         @Override
doInBackground(Void... params)75         protected Void doInBackground(Void... params) {
76             String[] services = listServices();
77             if (services == null) {
78                 Log.e(TAG, "There are no services, how odd");
79                 return null;
80             }
81             for (String service : services) {
82                 IBinder obj = checkService(service);
83                 if (obj != null) {
84                     Parcel data = Parcel.obtain();
85                     try {
86                         obj.transact(IBinder.SYSPROPS_TRANSACTION, data, null, 0);
87                     } catch (RemoteException e) {
88                         // Ignore
89                     } catch (Exception e) {
90                         Log.i(TAG, "Someone wrote a bad service '" + service
91                                 + "' that doesn't like to be poked", e);
92                     }
93                     data.recycle();
94                 }
95             }
96             return null;
97         }
98     }
99 }
100