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