11package io .virtualapp .home ;
22
33import android .app .Activity ;
4+ import android .content .ActivityNotFoundException ;
45import android .content .ComponentName ;
56import android .content .Context ;
67import android .content .Intent ;
8+ import android .net .Uri ;
9+ import android .os .Build ;
710import android .os .Bundle ;
811import android .os .Handler ;
12+ import android .os .PowerManager ;
913import android .os .SystemClock ;
14+ import android .preference .PreferenceManager ;
15+ import android .provider .Settings ;
16+ import android .support .v7 .app .AlertDialog ;
1017import android .text .TextUtils ;
1118import android .view .View ;
1219import android .widget .Toast ;
1320
1421import com .google .android .apps .nexuslauncher .NexusLauncherActivity ;
22+ import com .lody .virtual .client .core .VirtualCore ;
23+ import com .lody .virtual .helper .utils .DeviceUtil ;
1524
1625import java .io .File ;
1726import java .util .List ;
1827import java .util .concurrent .TimeUnit ;
1928
2029import io .virtualapp .R ;
30+ import io .virtualapp .VApp ;
2131import io .virtualapp .VCommends ;
2232import io .virtualapp .home .models .AppData ;
2333import io .virtualapp .home .models .AppInfoLite ;
3040
3141public class NewHomeActivity extends NexusLauncherActivity implements HomeContract .HomeView {
3242
43+ private static final String SHOW_DOZE_ALERT_KEY = "SHOW_DOZE_ALERT_KEY" ;
44+
3345 private HomeContract .HomePresenter mPresenter ;
3446 private Handler mUiHandler ;
3547 private int mInstallCount = 0 ;
36- private long mInstallStartTime ;
3748
3849
3950 @ Override
@@ -45,6 +56,9 @@ protected void onCreate(Bundle savedInstanceState) {
4556 getHotseat ().setSettingClickListener (v -> onSettingsClicked ());
4657
4758 new HomePresenterImpl (this ).start ();
59+
60+ alertForMeizu ();
61+ alertForDoze ();
4862 }
4963
5064 @ Override
@@ -204,4 +218,72 @@ private void refreshLoadingDialog(AppData model) {
204218 }
205219 });
206220 }
221+
222+ private void alertForMeizu () {
223+ if (!DeviceUtil .isMeizuBelowN ()) {
224+ return ;
225+ }
226+ boolean isXposedInstalled = VirtualCore .get ().isAppInstalled (VApp .XPOSED_INSTALLER_PACKAGE );
227+ if (isXposedInstalled ) {
228+ return ;
229+ }
230+ mUiHandler .postDelayed (() -> {
231+ AlertDialog alertDialog = new AlertDialog .Builder (getContext ())
232+ .setTitle (R .string .meizu_device_tips_title )
233+ .setMessage (R .string .meizu_device_tips_content )
234+ .setPositiveButton (android .R .string .yes , (dialog , which ) -> {
235+ })
236+ .create ();
237+ try {
238+ alertDialog .show ();
239+ } catch (Throwable ignored ) {}
240+ }, 2000 );
241+ }
242+
243+ private void alertForDoze () {
244+ if (Build .VERSION .SDK_INT < Build .VERSION_CODES .M ) {
245+ return ;
246+ }
247+ PowerManager powerManager = (PowerManager ) getSystemService (POWER_SERVICE );
248+ if (powerManager == null ) {
249+ return ;
250+ }
251+ boolean showAlert = PreferenceManager .getDefaultSharedPreferences (this ).getBoolean (SHOW_DOZE_ALERT_KEY , true );
252+ if (!showAlert ) {
253+ return ;
254+ }
255+ String packageName = getPackageName ();
256+ boolean ignoringBatteryOptimizations = powerManager .isIgnoringBatteryOptimizations (packageName );
257+ if (!ignoringBatteryOptimizations ) {
258+
259+ mUiHandler .postDelayed (() -> {
260+ AlertDialog alertDialog = new AlertDialog .Builder (getContext ())
261+ .setTitle (R .string .alert_for_doze_mode_title )
262+ .setMessage (R .string .alert_for_doze_mode_content )
263+ .setPositiveButton (R .string .alert_for_doze_mode_yes , (dialog , which ) -> {
264+ try {
265+ startActivity (new Intent (Settings .ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS , Uri .parse ("package:" + getPackageName ())));
266+ } catch (ActivityNotFoundException ignored ) {
267+ // ActivityNotFoundException on some devices.
268+ try {
269+ startActivity (new Intent (Settings .ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS ));
270+ } catch (Throwable e ) {
271+ PreferenceManager .getDefaultSharedPreferences (getActivity ())
272+ .edit ().putBoolean (SHOW_DOZE_ALERT_KEY , false ).apply ();
273+ }
274+ } catch (Throwable e ) {
275+ PreferenceManager .getDefaultSharedPreferences (getActivity ())
276+ .edit ().putBoolean (SHOW_DOZE_ALERT_KEY , false ).apply ();
277+ }
278+ })
279+ .setNegativeButton (R .string .alert_for_doze_mode_no , (dialog , which ) ->
280+ PreferenceManager .getDefaultSharedPreferences (getActivity ())
281+ .edit ().putBoolean (SHOW_DOZE_ALERT_KEY , false ).apply ())
282+ .create ();
283+ try {
284+ alertDialog .show ();
285+ } catch (Throwable ignored ) {}
286+ }, 3000 );
287+ }
288+ }
207289}
0 commit comments