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
17package com.android.settingslib.development;
18
19import android.os.AsyncTask;
20import android.os.IBinder;
21import android.os.Parcel;
22import android.os.RemoteException;
23import android.os.ServiceManager;
24import android.support.annotation.NonNull;
25import android.support.annotation.VisibleForTesting;
26import android.util.Log;
27
28public 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
35    private SystemPropPoker() {}
36
37    @NonNull
38    public static SystemPropPoker getInstance() {
39        return sInstance;
40    }
41
42    public void blockPokes() {
43        mBlockPokes = true;
44    }
45
46    public void unblockPokes() {
47        mBlockPokes = false;
48    }
49
50    public void poke() {
51        if (!mBlockPokes) {
52            createPokerTask().execute();
53        }
54    }
55
56    @VisibleForTesting
57    PokerTask createPokerTask() {
58        return new PokerTask();
59    }
60
61    public static class PokerTask extends AsyncTask<Void, Void, Void> {
62
63        @VisibleForTesting
64        String[] listServices() {
65            return ServiceManager.listServices();
66        }
67
68        @VisibleForTesting
69        IBinder checkService(String service) {
70            return ServiceManager.checkService(service);
71        }
72
73        @Override
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