Kaynağa Gözat

add more funtions for ksAndroid.Helpers

KngStr 5 yıl önce
ebeveyn
işleme
d698d2d6aa
1 değiştirilmiş dosya ile 111 ekleme ve 10 silme
  1. 111 10
      ksAndroid.Helpers.pas

+ 111 - 10
ksAndroid.Helpers.pas

@@ -4,7 +4,10 @@
 {                                                       }
 {       Copyright (C) 2020 KngStr                       }
 {                                                       }
-{   Some Code from Kastri Free of DelphiWorlds, Thanks  }
+{   Some Code from                                      }
+{     Kastri Free of DelphiWorlds                       }
+{     QDAC of swish                                     }
+{   Thanks                                              }
 {                                                       }
 {*******************************************************}
 
@@ -21,6 +24,7 @@ type
   private
     class function GetJActivity: JActivity; static;
     class function GetJContext: JContext; static;
+    class function GetJActivityManager: JActivityManager; static;
   public
     const
       ICE_CREAM_SANDWICH = 14;
@@ -90,20 +94,46 @@ type
     /// <summary>Returns Java Application Activity</summary>
     /// <remarks>An exception will be launched if there is no activity, for example a Service</remarks>
     class property Activity: JActivity read GetJActivity;
+    /// <summary>Returns Java Application Activity Manager</summary>
+    class property ActivityManager: JActivityManager read GetJActivityManager;
+
+    /// <remarks>Need reorder tasks permission</remarks>
+    class procedure BringAppToFront; static;
+    /// <remarks>Need reorder tasks permission</remarks>
+    class procedure SendAppToBack; static;
+    /// <summary>Call a Java Activity</summary>
+    class function StartActivity(Intent: JIntent; const Code: Integer = -1): Boolean; static;
 
     /// <summary>Returns Application package name</summary>
     class function GetPackageName: string; static;
+
+    /// <summary>Checks if there is at least one application capable of receiving the intent.</summary>
+    class function HasAssocApp(const URI: string): Boolean; overload; static;
+    /// <summary>Checks if there is at least one application capable of receiving the intent.</summary>
+    class function HasAssocApp(const Intent: JIntent): Boolean; overload; static;
+
+    /// <summary>Check if an application is installed</summary>
+    class function IsAppInstalled(const APackage: string): Boolean; static;
+
+    /// <summary>Add/Cear FLAG_KEEP_SCREEN_ON</summary>
+    class function KeepScreen(AOn: Boolean): Boolean; static;
   end;
 
 implementation
 
 uses
-  Androidapi.Helpers,
+  Androidapi.Helpers, Androidapi.JNIBridge,
   Androidapi.JNI.Os, Androidapi.JNI.Support,
-  Androidapi.JNI.Media, Androidapi.JNI.Provider;
+  Androidapi.JNI.Media, Androidapi.JNI.Provider,
+  FMX.Helpers.Android;
 
 { TAndroidHelperEx }
 
+class procedure TAndroidHelperEx.BringAppToFront;
+begin
+  ActivityManager.moveTaskToFront(Activity.getTaskId, TJIntent.JavaClass.FLAG_ACTIVITY_NEW_TASK);
+end;
+
 class function TAndroidHelperEx.CheckBuildAndTarget(const AValue: Integer): Boolean;
 begin
   Result := (GetBuildSdkVersion >= AValue) and (GetTargetSdkVersion >= AValue);
@@ -116,12 +146,12 @@ end;
 
 class function TAndroidHelperEx.GetClass(const APackageClassName: string): Jlang_Class;
 begin
-  Result := TJLang_Class.JavaClass.forName(StringToJString(APackageClassName), True, TAndroidHelper.Context.getClassLoader);
+  Result := TJLang_Class.JavaClass.forName(StringToJString(APackageClassName), True, Context.getClassLoader);
 end;
 
 class function TAndroidHelperEx.GetDefaultIconID: Integer;
 begin
-  Result := TAndroidHelper.Context.getApplicationInfo.icon;
+  Result := Context.getApplicationInfo.icon;
 end;
 
 class function TAndroidHelperEx.GetDefaultNotificationSound: Jnet_Uri;
@@ -139,6 +169,14 @@ begin
 {$ENDIF}
 end;
 
+class function TAndroidHelperEx.GetJActivityManager: JActivityManager;
+var
+  AService: JObject;
+begin
+  AService := Context.getSystemService(TJContext.JavaClass.ACTIVITY_SERVICE);
+  Result := TJActivityManager.Wrap((AService as ILocalObject).GetObjectID);
+end;
+
 class function TAndroidHelperEx.GetJContext: JContext;
 begin
   Result :=
@@ -158,10 +196,27 @@ class function TAndroidHelperEx.GetTargetSdkVersion: Integer;
 var
   LApplicationInfo: JApplicationInfo;
 begin
-  LApplicationInfo := TAndroidHelper.Context.getPackageManager.getApplicationInfo(TAndroidHelper.Context.getPackageName, 0);
+  LApplicationInfo := Context.getPackageManager.getApplicationInfo(Context.getPackageName, 0);
   Result := LApplicationInfo.targetSdkVersion;
 end;
 
+class function TAndroidHelperEx.HasAssocApp(const Intent: JIntent): Boolean;
+begin
+  Result :=  TAndroidHelper.Activity.getPackageManager.queryIntentActivities(
+    Intent, TJPackageManager.JavaClass.MATCH_DEFAULT_ONLY).size > 0;
+end;
+
+class function TAndroidHelperEx.HasAssocApp(const URI: string): Boolean;
+var
+  Intent: JIntent;
+begin
+  Result := False;
+  Intent := TJIntent.Create;
+  Intent.setData(TJnet_Uri.JavaClass.parse(StringToJString(URI)));
+  Intent.setAction(StringToJString('android.intent.action.VIEW'));
+  Result := HasAssocApp(Intent);
+end;
+
 class function TAndroidHelperEx.IsActivityForeground: Boolean;
 var
   LService: JObject;
@@ -170,7 +225,7 @@ var
   I: Integer;
 begin
   Result := False;
-  LService := TAndroidHelper.Context.getSystemService(TJContext.JavaClass.ACTIVITY_SERVICE);
+  LService := Context.getSystemService(TJContext.JavaClass.ACTIVITY_SERVICE);
   LRunningApps := TJActivityManager.Wrap(TAndroidHelper.JObjectToID(LService)).getRunningAppProcesses;
   for I := 0 to LRunningApps.size - 1 do
   begin
@@ -179,7 +234,7 @@ begin
     begin
       if LAppInfo.importanceReasonComponent <> nil then
       begin
-        if LAppInfo.importanceReasonComponent.getPackageName.equals(TAndroidHelper.Context.getPackageName) then
+        if LAppInfo.importanceReasonComponent.getPackageName.equals(Context.getPackageName) then
           Exit(True);
       end
       else if LRunningApps.size = 1 then
@@ -188,14 +243,60 @@ begin
   end;
 end;
 
+class function TAndroidHelperEx.IsAppInstalled(const APackage: string): Boolean;
+begin
+  Result := False;
+  try
+    //只有异常是可靠的,返回值判定不对
+    Result := Context.getPackageManager.getPackageInfo(StringToJString(S),
+      TJPackageManager.JavaClass.GET_ACTIVITIES) = nil;
+    Result := True;
+  except
+  end;
+end;
+
+class function TAndroidHelperEx.KeepScreen(AOn: Boolean): Boolean;
+begin
+  CallInUIThreadAndWaitFinishing(
+    procedure
+    begin
+      if AOn then
+        SharedActivity.getWindow.addFlags(
+          TJWindowManager_LayoutParams.JavaClass.FLAG_KEEP_SCREEN_ON)
+      else
+        SharedActivity.getWindow.clearFlags(
+          TJWindowManager_LayoutParams.JavaClass.FLAG_KEEP_SCREEN_ON);
+    end);
+end;
+
+class procedure TAndroidHelperEx.SendAppToBack;
+begin
+  //SharedActivityManager.moveTaskToBack(Activity.getTaskId, TJIntent.JavaClass.FLAG_ACTIVITY_NEW_TASK);
+  Activity.moveTaskToBack(True);
+end;
+
+class function TAndroidHelperEx.StartActivity(Intent: JIntent; const Code: Integer): Boolean;
+begin
+  Result := False;
+  //Checks if there is at least one application capable of receiving the intent.
+  if Activity.getPackageManager.queryIntentActivities(Intent,
+      TJPackageManager.JavaClass.MATCH_DEFAULT_ONLY).size > 0 then begin
+    if Code = -1 then
+      Activity.startActivity(Intent)
+    else
+      Activity.startActivityForResult(Intent, Code);
+    Result := True;
+  end;
+end;
+
 class function TAndroidHelperEx.UriFromFile(const AFile: JFile): Jnet_Uri;
 var
   LAuthority: JString;
 begin
   if CheckBuildAndTarget(NOUGAT) then
   begin
-    LAuthority := StringToJString(JStringToString(TAndroidHelper.Context.getApplicationContext.getPackageName) + '.fileprovider');
-    Result := TJFileProvider.JavaClass.getUriForFile(TAndroidHelper.Context, LAuthority, AFile);
+    LAuthority := StringToJString(JStringToString(Context.getApplicationContext.getPackageName) + '.fileprovider');
+    Result := TJFileProvider.JavaClass.getUriForFile(Context, LAuthority, AFile);
   end
   else
     Result := TJnet_uri.JavaClass.fromFile(AFile);