liyujie
2025-08-28 b3810562527858a3b3d98ffa6e9c9c5b0f4a9a8e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package android.aw;
 
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.util.Log;
import android.util.Singleton;
 
import java.util.List;
 
/**
 * @hide
 */
public final class BackgroundManager {
    private static final String TAG = "BackgroundManager";
 
    public static final String SERVICE = "background";
 
    /** @hide */
    public BackgroundManager() {
    }
 
    public static IBackgroundManager getService() {
        return IBackgroundManagerSingleton.get();
    }
 
    private static final Singleton<IBackgroundManager> IBackgroundManagerSingleton =
            new Singleton<IBackgroundManager>() {
                @Override
                protected IBackgroundManager create() {
                    final IBinder b = ServiceManager.getService(SERVICE);
                    final IBackgroundManager am = IBackgroundManager.Stub.asInterface(b);
                    return am;
                }
            };
 
    /**
     * set debug
     */
    public static void setDebug(boolean debug) {
        IBackgroundManager bm = getService();
        if (bm == null) return;
        try {
            bm.setDebug(debug);
        } catch (RemoteException e) {
            Log.w(TAG, "setDebug error", e);
        }
    }
 
    /**
     * set current inputmethod
     * @param inputmethod current default inputmethod
     */
    public static void setCurrentInputMethod(String inputmethod) {
        IBackgroundManager bm = getService();
        if (bm == null) return;
        try {
            bm.setCurrentInputMethod(inputmethod);
        } catch (RemoteException e) {
            Log.w(TAG, "setCurrentInputMethod error", e);
        }
    }
 
    /**
     * set user whitelist
     * @param whitelist user whitelist
     */
    public static void setUserWhitelist(List<String> whitelist) {
        IBackgroundManager bm = getService();
        if (bm == null) return;
        try {
            bm.setUserWhitelist(whitelist);
        } catch (RemoteException e) {
            Log.w(TAG, "setUserWhitelist error", e);
        }
    }
 
    /**
     * return user whitelist
     */
    public static List<String> getSystemWhitelist() {
        IBackgroundManager bm = getService();
        if (bm == null) return null;
        try {
            return bm.getSystemWhitelist();
        } catch (RemoteException e) {
            Log.w(TAG, "getSystemWhitelist error", e);
            return null;
        }
    }
 
    /**
     * set limit background count
     * @param limit background count
     */
    public static void setLimitBackgroundCount(int limit) {
        IBackgroundManager bm = getService();
        if (bm == null) return;
        try {
            bm.setLimitBackgroundCount(limit);
        } catch (RemoteException e) {
            Log.w(TAG, "setLimitBackgroundCount error", e);
        }
    }
 
    /**
     * return limit background count
     */
    public static int getLimitBackgroundCount() {
        IBackgroundManager bm = getService();
        if (bm == null) return -1;
        try {
            return bm.getLimitBackgroundCount();
        } catch (RemoteException e) {
            Log.w(TAG, "getLimitBackgroundCount error", e);
            return -1;
        }
    }
 
    /**
     * check skip service
     * @param Intent service intent
     */
    public static boolean skipService(Intent service) {
        IBackgroundManager bm = getService();
        if (bm == null) return false;
        try {
            return bm.skipService(service);
        } catch (RemoteException e) {
            Log.w(TAG, "skipService " + service + " error", e);
            return false;
        }
    }
 
    /**
     * resolver receivers and remove not in whitelist
     * @param Intent target intent
     * @param receivers resolver receivers
     */
    public static String[] resolverReceiver(Intent intent, List<ResolveInfo> receivers) {
        IBackgroundManager bm = getService();
        if (bm == null) return null;
        try {
            return bm.resolverReceiver(intent, receivers);
        } catch (RemoteException e) {
            Log.w(TAG, "resolverReceiver " + intent + " error", e);
            return null;
        }
    }
}