exTHmUI-11 ready

Signed-off-by: cjybyjk <cjybyjk@zjnu.edu.cn>
Change-Id: I91ec0793764f7459e2bcff92dfccfd37b2c365f4
diff --git a/app/src/main/java/org/exthmui/game/qs/ScreenCaptureTile.java b/app/src/main/java/org/exthmui/game/qs/ScreenCaptureTile.java
index ebb4bb2..bc1cc47 100644
--- a/app/src/main/java/org/exthmui/game/qs/ScreenCaptureTile.java
+++ b/app/src/main/java/org/exthmui/game/qs/ScreenCaptureTile.java
@@ -16,14 +16,18 @@
 
 package org.exthmui.game.qs;
 
+import static android.view.WindowManager.ScreenshotSource.SCREENSHOT_GLOBAL_ACTIONS;
+
 import android.content.Context;
 import android.content.Intent;
 import android.os.Bundle;
 import android.os.Handler;
-import android.os.RemoteException;
+import android.os.Looper;
 import android.util.Log;
 import android.view.View;
-import android.view.WindowManagerGlobal;
+import android.view.WindowManager;
+
+import com.android.internal.util.ScreenshotHelper;
 
 import androidx.localbroadcastmanager.content.LocalBroadcastManager;
 
@@ -34,12 +38,16 @@
 
     private static final String TAG = "ScreenCaptureTile";
 
-    private Intent hideMenuIntent = new Intent(Constants.Broadcasts.BROADCAST_GAMING_MENU_CONTROL).putExtra("cmd", "hide");
-    private Handler mHandler = new Handler();
+    private static final Intent hideMenuIntent = new Intent(Constants.Broadcasts.BROADCAST_GAMING_MENU_CONTROL).putExtra("cmd", "hide");
+    
+    private Handler mHandler;
+    private ScreenshotHelper mScreenshotHelper;
 
     public ScreenCaptureTile(Context context) {
         super(context, context.getString(R.string.qs_screen_capture), "", R.drawable.ic_qs_screenshot);
         qsIcon.setSelected(true);
+        mHandler = new Handler(Looper.getMainLooper());
+        mScreenshotHelper = new ScreenshotHelper(context);
     }
 
     @Override
@@ -52,8 +60,9 @@
         LocalBroadcastManager.getInstance(mContext).sendBroadcastSync(hideMenuIntent);
         mHandler.postDelayed(() -> {
             try {
-                WindowManagerGlobal.getWindowManagerService().takeAlternativeScreenshot();
-            } catch (RemoteException e) {
+                mScreenshotHelper.takeScreenshot(WindowManager.TAKE_SCREENSHOT_FULLSCREEN, true, true,
+                    SCREENSHOT_GLOBAL_ACTIONS, mHandler, null);
+            } catch (Exception e) {
                 Log.e(TAG, "Error while trying to take screenshot.", e);
             }
         }, 500);
diff --git a/app/src/main/java/org/exthmui/game/services/GamingService.java b/app/src/main/java/org/exthmui/game/services/GamingService.java
index 272c659..8df2ff9 100644
--- a/app/src/main/java/org/exthmui/game/services/GamingService.java
+++ b/app/src/main/java/org/exthmui/game/services/GamingService.java
@@ -23,14 +23,19 @@
 import android.app.Service;
 import android.content.BroadcastReceiver;
 import android.content.ComponentName;
+import android.content.ContentResolver;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
+import android.database.ContentObserver;
 import android.media.AudioDeviceInfo;
 import android.media.AudioManager;
 import android.media.AudioSystem;
+import android.net.Uri;
 import android.os.Bundle;
+import android.os.Handler;
 import android.os.IBinder;
+import android.os.Looper;
 import android.os.RemoteException;
 import android.os.ServiceManager;
 import android.os.SystemProperties;
@@ -81,6 +86,8 @@
     private TelecomManager mTelecomManager;
 
     private GamingPhoneStateListener mPhoneStateListener;
+    
+    private GamingModeStatusObserver mGamingModeStatusObserver;
 
     private BroadcastReceiver mGamingModeOffReceiver = new BroadcastReceiver() {
         @Override
@@ -128,7 +135,6 @@
         }
     };
 
-
     public GamingService() {
     }
 
@@ -153,6 +159,7 @@
         registerReceiver(mGamingModeOffReceiver, new IntentFilter(Constants.Broadcasts.SYS_BROADCAST_GAMING_MODE_OFF));
         LocalBroadcastManager.getInstance(this).registerReceiver(mCallControlReceiver, new IntentFilter(Constants.Broadcasts.BROADCAST_CALL_CONTROL));
         LocalBroadcastManager.getInstance(this).registerReceiver(mGamingActionReceiver, new IntentFilter(Constants.Broadcasts.BROADCAST_GAMING_ACTION));
+        mGamingModeStatusObserver = new GamingModeStatusObserver(this, new Handler(Looper.getMainLooper()));
 
         mPhoneStateListener = new GamingPhoneStateListener();
         mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
@@ -427,4 +434,30 @@
         }
     }
 
+    private static class GamingModeStatusObserver extends ContentObserver {
+
+        private static Uri mUriForIsGaming = Settings.System.getUriFor(Settings.System.GAMING_MODE_ACTIVE);
+
+        private Service mService;
+        private ContentResolver mResolver;
+
+        public GamingModeStatusObserver(Service service, Handler handler) {
+            super(handler);
+            mService = service;
+            mResolver = service.getContentResolver();
+            mResolver.registerContentObserver(mUriForIsGaming, false, this);
+
+        }
+
+        @Override
+        public void onChange(boolean selfChange, Uri uri) {
+            super.onChange(selfChange, uri);
+            if (mUriForIsGaming.equals(uri)) {
+                if (Settings.System.getInt(mResolver, Settings.System.GAMING_MODE_ACTIVE, 0) == 0) {
+                    mResolver.unregisterContentObserver(this);
+                    mService.stopSelf();
+                }
+            }
+        }
+    }
 }