Ver Fonte

add new files

KngStr há 5 anos atrás
pai
commit
f161b6600d
2 ficheiros alterados com 336 adições e 0 exclusões
  1. 209 0
      ksAndroid.Helpers.pas
  2. 127 0
      ksString.pas

+ 209 - 0
ksAndroid.Helpers.pas

@@ -0,0 +1,209 @@
+{*******************************************************}
+{                                                       }
+{       Helpers for Android                             }
+{                                                       }
+{       Copyright (C) 2020 KngStr                       }
+{                                                       }
+{   Some Code from Kastri Free of DelphiWorlds, Thanks  }
+{                                                       }
+{*******************************************************}
+
+unit ksAndroid.Helpers;
+
+interface
+
+uses
+  Androidapi.JNI.GraphicsContentViewText, Androidapi.JNI.App,
+  Androidapi.JNI.JavaTypes, Androidapi.JNI.Net;
+
+type
+  TAndroidHelperEx = record
+  private
+    class function GetJActivity: JActivity; static;
+    class function GetJContext: JContext; static;
+  public
+    const
+      ICE_CREAM_SANDWICH = 14;
+      ICE_CREAM_SANDWICH_MR1 = 15;
+      JELLY_BEAN = 16;
+      JELLY_BEAN_MR1 = 17;
+      JELLY_BEAN_MR2 = 18;
+      KITKAT = 19;
+      KITKAT_MR1 = 20;
+      LOLLIPOP = 21;
+      LOLLIPOP_MR1 = 22;
+      MARSHMALLOW = 23;
+      NOUGAT = 24;
+      NOUGAT_MR1 = 25;
+      OREO = 26;
+      OREO_MR1 = 27;
+      PIE = 28;
+      Q = 29;
+    /// <summary>
+    ///   Checks if both build and target are greater or equal to the tested value
+    /// </summary>
+    class function CheckBuildAndTarget(const AValue: Integer): Boolean; static;
+    /// <summary>
+    ///   Returns the equivalent of "AndroidClass.class"
+    /// </summary>
+    class function GetClass(const APackageClassName: string): Jlang_Class; static;
+    /// <summary>
+    ///   Returns the application default icon ID
+    /// </summary>
+    class function GetDefaultIconID: Integer; static;
+    /// <summary>
+    ///   Returns a URI to the notification sound
+    /// </summary>
+    class function GetDefaultNotificationSound: Jnet_Uri; static;
+    /// <summary>
+    ///   Returns target Sdk version
+    /// </summary>
+    class function GetTargetSdkVersion: Integer; static;
+    /// <summary>
+    ///   Returns installed Sdk version
+    /// </summary>
+    class function GetBuildSdkVersion: Integer; static;
+    /// <summary>
+    ///   Returns whether the activity is running foreground
+    /// </summary>
+    /// <remarks>
+    ///   Useful from within a service to determine whether or not the service needs to run in foreground mode
+    /// </remarks>
+    class function IsActivityForeground: Boolean; static;
+    /// <summary>
+    ///   Converts file to uri, using FileProvider if target API >= 24
+    /// </summary>
+    /// <remarks>
+    ///   Use this only when accessing files with an "external" URI
+    /// </remarks>
+    class function UriFromFile(const AFile: JFile): Jnet_Uri; static;
+    /// <summary>
+    ///   Converts filename to uri, using FileProvider if target API >= 24
+    /// </summary>
+    /// <remarks>
+    ///   Use this only when accessing files with an "external" URI
+    /// </remarks>
+    class function UriFromFileName(const AFileName: string): Jnet_Uri; static;
+
+    /// <summary>Returns Java Application Context</summary>
+    class property Context: JContext read GetJContext;
+    /// <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 Application package name</summary>
+    class function GetPackageName: string; static;
+  end;
+
+implementation
+
+uses
+  Androidapi.Helpers,
+  Androidapi.JNI.Os, Androidapi.JNI.Support,
+  Androidapi.JNI.Media, Androidapi.JNI.Provider;
+
+{ TAndroidHelperEx }
+
+class function TAndroidHelperEx.CheckBuildAndTarget(const AValue: Integer): Boolean;
+begin
+  Result := (GetBuildSdkVersion >= AValue) and (GetTargetSdkVersion >= AValue);
+end;
+
+class function TAndroidHelperEx.GetBuildSdkVersion: Integer;
+begin
+   Result := TJBuild_VERSION.JavaClass.SDK_INT;
+end;
+
+class function TAndroidHelperEx.GetClass(const APackageClassName: string): Jlang_Class;
+begin
+  Result := TJLang_Class.JavaClass.forName(StringToJString(APackageClassName), True, TAndroidHelper.Context.getClassLoader);
+end;
+
+class function TAndroidHelperEx.GetDefaultIconID: Integer;
+begin
+  Result := TAndroidHelper.Context.getApplicationInfo.icon;
+end;
+
+class function TAndroidHelperEx.GetDefaultNotificationSound: Jnet_Uri;
+begin
+  Result := TJRingtoneManager.JavaClass.getDefaultUri(TJRingtoneManager.JavaClass.TYPE_NOTIFICATION);
+end;
+
+class function TAndroidHelperEx.GetJActivity: JActivity;
+begin
+  Result :=
+{$IF CompilerVersion > 27}
+  TAndroidHelper.Activity
+{$ELSE}
+  SharedActivity
+{$ENDIF}
+end;
+
+class function TAndroidHelperEx.GetJContext: JContext;
+begin
+  Result :=
+{$IF CompilerVersion > 27}
+  TAndroidHelper.Context;
+{$ELSE}
+  SharedActivityContext;
+{$ENDIF}
+end;
+
+class function TAndroidHelperEx.GetPackageName: string;
+begin
+  Result := JStringToString(Context.getPackageName);
+end;
+
+class function TAndroidHelperEx.GetTargetSdkVersion: Integer;
+var
+  LApplicationInfo: JApplicationInfo;
+begin
+  LApplicationInfo := TAndroidHelper.Context.getPackageManager.getApplicationInfo(TAndroidHelper.Context.getPackageName, 0);
+  Result := LApplicationInfo.targetSdkVersion;
+end;
+
+class function TAndroidHelperEx.IsActivityForeground: Boolean;
+var
+  LService: JObject;
+  LRunningApps: JList;
+  LAppInfo: JActivityManager_RunningAppProcessInfo;
+  I: Integer;
+begin
+  Result := False;
+  LService := TAndroidHelper.Context.getSystemService(TJContext.JavaClass.ACTIVITY_SERVICE);
+  LRunningApps := TJActivityManager.Wrap(TAndroidHelper.JObjectToID(LService)).getRunningAppProcesses;
+  for I := 0 to LRunningApps.size - 1 do
+  begin
+    LAppInfo := TJActivityManager_RunningAppProcessInfo.Wrap(TAndroidHelper.JObjectToID(LRunningApps.get(I)));
+    if LAppInfo.importance = 100 then
+    begin
+      if LAppInfo.importanceReasonComponent <> nil then
+      begin
+        if LAppInfo.importanceReasonComponent.getPackageName.equals(TAndroidHelper.Context.getPackageName) then
+          Exit(True);
+      end
+      else if LRunningApps.size = 1 then
+        Exit(True);
+    end;
+  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);
+  end
+  else
+    Result := TJnet_uri.JavaClass.fromFile(AFile);
+end;
+
+class function TAndroidHelperEx.UriFromFileName(const AFileName: string): Jnet_Uri;
+begin
+  Result := UriFromFile(TJFile.JavaClass.init(StringToJString(AFileName)));
+end;
+
+end.

+ 127 - 0
ksString.pas

@@ -0,0 +1,127 @@
+{*******************************************************}
+{                                                       }
+{       Methods of Sting                                }
+{                                                       }
+{       版权所有 (C) 2020 KngStr                        }
+{                                                       }
+{*******************************************************}
+
+unit ksString;
+
+interface
+
+/// <summary>
+/// 创建空白文件
+/// </summary>
+function CreateBlankFile(S: string): Boolean;
+/// <summary>
+/// 自动为已存在文件名加数字
+/// </summary>
+function AutoRenameFileName(AFile: string): string;
+/// <summary>
+/// 自动替换文件名中的非法字符为
+/// </summary>
+procedure ReplaceInvalidFileNameChars(var FileName: string; AChar: Char = '_');
+
+/// <summary>
+/// 格式化秒为时间,冒号分割
+/// </summary>
+function FormatSeconds(const AValue: Int64): string; overload;
+/// <summary>
+/// 格式化秒为时间,冒号分割
+/// </summary>
+function FormatSeconds(const AValue: Integer): string; overload;
+/// <summary>
+/// 格式化秒为时间,冒号分割
+/// </summary>
+function FormatSeconds(const AValue: Double): string; overload;
+
+implementation
+
+uses
+  System.Classes, System.SysUtils, System.IOUtils;
+
+function CreateBlankFile(S: string): Boolean;
+var
+  FStrm: TFileStream;
+begin
+  Result := False;
+  try
+    FStrm := TFileStream.Create(S, fmCreate);
+    if FStrm = nil then
+      Exit;
+    Result := True;
+    FreeAndNil(FStrm);
+  except
+    FStrm := nil;
+  end;
+end;
+
+function AutoRenameFileName(AFile: string): string;
+var
+  I: Integer;
+  sPath, sExt: string;
+begin
+  Result := AFile;
+  I := 0;
+  sPath := ChangeFileExt(Result, '');
+  sExt := ExtractFileExt(Result);
+  while FileExists(Result) do begin
+    Inc(I);
+    Result := Format('%s(%d)%s', [sPath, I, sExt]);
+  end;
+end;
+
+procedure ReplaceInvalidFileNameChars(var FileName: string; AChar: Char);
+var
+  PFileName: PChar;
+  FileNameLen: Integer;
+  Ch: Char;
+  I: Integer;
+begin
+  I := 0;
+  PFileName := PChar(FileName);
+  FileNameLen := Length(FileName);
+  while I < FileNameLen do begin
+    Ch := PFileName[I];
+
+    if not TPath.IsValidFileNameChar(Ch) then
+      PFileName[I] := AChar
+    else
+      Inc(I);
+  end;
+end;
+
+function FormatSeconds(const AValue: Double): string; overload;
+var
+  h, m, s, ms: Integer;
+begin
+  ms := Trunc(Frac(AValue) * 1000);
+  s := Trunc(AValue);
+  m := s div 60;
+  h := m div 60;
+  if h > 0 then
+    Result := Format('%.2d:%.2d:%.2d.%.3d', [h, m mod 60, s mod 60, ms])
+  else
+    Result := Format('%.2d:%.2d.%.3d', [m, s mod 60, ms]);
+end;
+
+function FormatSeconds(const AValue: Int64): string;
+var
+  h, m, s: Integer;
+begin
+  s := AValue;
+  m := s div 60;
+  h := m div 60;
+  if h > 0 then
+    Result := Format('%.2d:%.2d:%.2d', [h, m mod 60, s mod 60])
+  else
+    Result := Format('%.2d:%.2d', [m, s mod 60]);
+end;
+
+function FormatSeconds(const AValue: Integer): string; overload;
+begin
+  FormatSeconds(Int64(AValue));
+end;
+
+end.