JLChen
2021-01-04 ed86fe7ed952bb2151aac8841134246bbde152c9
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
 * Copyright (c) 2010-2019 Belledonne Communications SARL.
 *
 * This file is part of linphone-android
 * (see https://www.linphone.org).
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.linphone.utils;
 
import android.app.Dialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import androidx.core.content.ContextCompat;
import java.util.List;
import org.linphone.R;
import org.linphone.compatibility.Compatibility;
import org.linphone.core.tools.Log;
import org.linphone.settings.LinphonePreferences;
 
public class DeviceUtils {
    private static final Intent[] POWERMANAGER_INTENTS = {
        new Intent()
                .setComponent(
                        new ComponentName(
                                "com.miui.securitycenter",
                                "com.miui.permcenter.autostart.AutoStartManagementActivity")),
        new Intent()
                .setComponent(
                        new ComponentName(
                                "com.letv.android.letvsafe",
                                "com.letv.android.letvsafe.AutobootManageActivity")),
        new Intent()
                .setComponent(
                        new ComponentName(
                                "com.huawei.systemmanager",
                                "com.huawei.systemmanager.appcontrol.activity.StartupAppControlActivity")),
        new Intent()
                .setComponent(
                        new ComponentName(
                                "com.huawei.systemmanager",
                                "com.huawei.systemmanager.optimize.process.ProtectActivity")),
        new Intent()
                .setComponent(
                        new ComponentName(
                                "com.coloros.safecenter",
                                "com.coloros.safecenter.permission.startup.StartupAppListActivity")),
        new Intent()
                .setComponent(
                        new ComponentName(
                                "com.coloros.safecenter",
                                "com.coloros.safecenter.startupapp.StartupAppListActivity")),
        new Intent()
                .setComponent(
                        new ComponentName(
                                "com.oppo.safe",
                                "com.oppo.safe.permission.startup.StartupAppListActivity")),
        new Intent()
                .setComponent(
                        new ComponentName(
                                "com.iqoo.secure",
                                "com.iqoo.secure.ui.phoneoptimize.AddWhiteListActivity")),
        new Intent()
                .setComponent(
                        new ComponentName(
                                "com.iqoo.secure",
                                "com.iqoo.secure.ui.phoneoptimize.BgStartUpManager")),
        new Intent()
                .setComponent(
                        new ComponentName(
                                "com.vivo.permissionmanager",
                                "com.vivo.permissionmanager.activity.BgStartUpManagerActivity")),
        new Intent()
                .setComponent(
                        new ComponentName(
                                "com.samsung.android.lool",
                                "com.samsung.android.sm.ui.battery.BatteryActivity")),
        new Intent()
                .setComponent(
                        new ComponentName(
                                "com.htc.pitroad",
                                "com.htc.pitroad.landingpage.activity.LandingPageActivity")),
        new Intent()
                .setComponent(
                        new ComponentName(
                                "com.asus.mobilemanager", "com.asus.mobilemanager.MainActivity")),
        new Intent()
                .setComponent(
                        new ComponentName(
                                "com.asus.mobilemanager",
                                "com.asus.mobilemanager.autostart.AutoStartActivity")),
        new Intent()
                .setComponent(
                        new ComponentName(
                                "com.asus.mobilemanager",
                                "com.asus.mobilemanager.entry.FunctionActivity"))
                .setData(Uri.parse("mobilemanager://function/entry/AutoStart")),
        new Intent()
                .setComponent(
                        new ComponentName(
                                "com.dewav.dwappmanager",
                                "com.dewav.dwappmanager.memory.SmartClearupWhiteList"))
    };
 
    public static Intent getDevicePowerManagerIntent(Context context) {
        for (Intent intent : POWERMANAGER_INTENTS) {
            if (DeviceUtils.isIntentCallable(context, intent)) {
                return intent;
            }
        }
        return null;
    }
 
    public static boolean hasDevicePowerManager(Context context) {
        return getDevicePowerManagerIntent(context) != null;
    }
 
    public static boolean isAppUserRestricted(Context context) {
        return Compatibility.isAppUserRestricted(context);
    }
 
    public static int getAppStandbyBucket(Context context) {
        return Compatibility.getAppStandbyBucket(context);
    }
 
    public static void displayDialogIfDeviceHasPowerManagerThatCouldPreventPushNotifications(
            final Context context) {
        for (final Intent intent : POWERMANAGER_INTENTS) {
            if (DeviceUtils.isIntentCallable(context, intent)) {
                Log.w(
                        "[Hacks] ",
                        android.os.Build.MANUFACTURER,
                        " device with power saver detected: ",
                        intent.getComponent().getClassName());
                if (!LinphonePreferences.instance().hasPowerSaverDialogBeenPrompted()) {
                    Log.w("[Hacks] Asking power saver for whitelist !");
 
                    final Dialog dialog = new Dialog(context);
                    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                    Drawable d =
                            new ColorDrawable(
                                    ContextCompat.getColor(context, R.color.dark_grey_color));
                    d.setAlpha(200);
                    dialog.setContentView(R.layout.dialog);
                    dialog.getWindow()
                            .setLayout(
                                    WindowManager.LayoutParams.MATCH_PARENT,
                                    WindowManager.LayoutParams.MATCH_PARENT);
                    dialog.getWindow().setBackgroundDrawable(d);
 
                    TextView customText = dialog.findViewById(R.id.dialog_message);
                    customText.setText(R.string.device_power_saver_dialog_message);
 
                    TextView customTitle = dialog.findViewById(R.id.dialog_title);
                    customTitle.setText(R.string.device_power_saver_dialog_title);
 
                    dialog.findViewById(R.id.dialog_do_not_ask_again_layout)
                            .setVisibility(View.VISIBLE);
                    final CheckBox doNotAskAgain = dialog.findViewById(R.id.doNotAskAgain);
                    dialog.findViewById(R.id.doNotAskAgainLabel)
                            .setOnClickListener(
                                    new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            doNotAskAgain.setChecked(!doNotAskAgain.isChecked());
                                        }
                                    });
 
                    Button accept = dialog.findViewById(R.id.dialog_ok_button);
                    accept.setVisibility(View.VISIBLE);
                    accept.setText(R.string.device_power_saver_dialog_button_go_to_settings);
                    accept.setOnClickListener(
                            new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Log.w(
                                            "[Hacks] Power saver detected, user is going to settings :)");
                                    // If user is going into the settings,
                                    // assume it will make the change so don't prompt again
                                    LinphonePreferences.instance().powerSaverDialogPrompted(true);
 
                                    try {
                                        context.startActivity(intent);
                                    } catch (SecurityException se) {
                                        Log.e(
                                                "[Hacks] Couldn't start intent [",
                                                intent.getComponent().getClassName(),
                                                "], security exception was thrown: ",
                                                se);
                                    }
                                    dialog.dismiss();
                                }
                            });
 
                    Button cancel = dialog.findViewById(R.id.dialog_cancel_button);
                    cancel.setText(R.string.device_power_saver_dialog_button_later);
                    cancel.setOnClickListener(
                            new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Log.w(
                                            "[Hacks] Power saver detected, user didn't go to settings :(");
                                    if (doNotAskAgain.isChecked()) {
                                        LinphonePreferences.instance()
                                                .powerSaverDialogPrompted(true);
                                    }
                                    dialog.dismiss();
                                }
                            });
 
                    Button delete = dialog.findViewById(R.id.dialog_delete_button);
                    delete.setVisibility(View.GONE);
 
                    dialog.show();
                }
            }
        }
    }
 
    private static boolean isIntentCallable(Context context, Intent intent) {
        List<ResolveInfo> list =
                context.getPackageManager()
                        .queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
        return list.size() > 0;
    }
}