ksAndroid.Helpers.pas 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. {*******************************************************}
  2. { }
  3. { Helpers for Android }
  4. { }
  5. { Copyright (C) 2020 KngStr }
  6. { }
  7. { Some Code from Kastri Free of DelphiWorlds, Thanks }
  8. { }
  9. {*******************************************************}
  10. unit ksAndroid.Helpers;
  11. interface
  12. uses
  13. Androidapi.JNI.GraphicsContentViewText, Androidapi.JNI.App,
  14. Androidapi.JNI.JavaTypes, Androidapi.JNI.Net;
  15. type
  16. TAndroidHelperEx = record
  17. private
  18. class function GetJActivity: JActivity; static;
  19. class function GetJContext: JContext; static;
  20. public
  21. const
  22. ICE_CREAM_SANDWICH = 14;
  23. ICE_CREAM_SANDWICH_MR1 = 15;
  24. JELLY_BEAN = 16;
  25. JELLY_BEAN_MR1 = 17;
  26. JELLY_BEAN_MR2 = 18;
  27. KITKAT = 19;
  28. KITKAT_MR1 = 20;
  29. LOLLIPOP = 21;
  30. LOLLIPOP_MR1 = 22;
  31. MARSHMALLOW = 23;
  32. NOUGAT = 24;
  33. NOUGAT_MR1 = 25;
  34. OREO = 26;
  35. OREO_MR1 = 27;
  36. PIE = 28;
  37. Q = 29;
  38. /// <summary>
  39. /// Checks if both build and target are greater or equal to the tested value
  40. /// </summary>
  41. class function CheckBuildAndTarget(const AValue: Integer): Boolean; static;
  42. /// <summary>
  43. /// Returns the equivalent of "AndroidClass.class"
  44. /// </summary>
  45. class function GetClass(const APackageClassName: string): Jlang_Class; static;
  46. /// <summary>
  47. /// Returns the application default icon ID
  48. /// </summary>
  49. class function GetDefaultIconID: Integer; static;
  50. /// <summary>
  51. /// Returns a URI to the notification sound
  52. /// </summary>
  53. class function GetDefaultNotificationSound: Jnet_Uri; static;
  54. /// <summary>
  55. /// Returns target Sdk version
  56. /// </summary>
  57. class function GetTargetSdkVersion: Integer; static;
  58. /// <summary>
  59. /// Returns installed Sdk version
  60. /// </summary>
  61. class function GetBuildSdkVersion: Integer; static;
  62. /// <summary>
  63. /// Returns whether the activity is running foreground
  64. /// </summary>
  65. /// <remarks>
  66. /// Useful from within a service to determine whether or not the service needs to run in foreground mode
  67. /// </remarks>
  68. class function IsActivityForeground: Boolean; static;
  69. /// <summary>
  70. /// Converts file to uri, using FileProvider if target API >= 24
  71. /// </summary>
  72. /// <remarks>
  73. /// Use this only when accessing files with an "external" URI
  74. /// </remarks>
  75. class function UriFromFile(const AFile: JFile): Jnet_Uri; static;
  76. /// <summary>
  77. /// Converts filename to uri, using FileProvider if target API >= 24
  78. /// </summary>
  79. /// <remarks>
  80. /// Use this only when accessing files with an "external" URI
  81. /// </remarks>
  82. class function UriFromFileName(const AFileName: string): Jnet_Uri; static;
  83. /// <summary>Returns Java Application Context</summary>
  84. class property Context: JContext read GetJContext;
  85. /// <summary>Returns Java Application Activity</summary>
  86. /// <remarks>An exception will be launched if there is no activity, for example a Service</remarks>
  87. class property Activity: JActivity read GetJActivity;
  88. /// <summary>Returns Application package name</summary>
  89. class function GetPackageName: string; static;
  90. end;
  91. implementation
  92. uses
  93. Androidapi.Helpers,
  94. Androidapi.JNI.Os, Androidapi.JNI.Support,
  95. Androidapi.JNI.Media, Androidapi.JNI.Provider;
  96. { TAndroidHelperEx }
  97. class function TAndroidHelperEx.CheckBuildAndTarget(const AValue: Integer): Boolean;
  98. begin
  99. Result := (GetBuildSdkVersion >= AValue) and (GetTargetSdkVersion >= AValue);
  100. end;
  101. class function TAndroidHelperEx.GetBuildSdkVersion: Integer;
  102. begin
  103. Result := TJBuild_VERSION.JavaClass.SDK_INT;
  104. end;
  105. class function TAndroidHelperEx.GetClass(const APackageClassName: string): Jlang_Class;
  106. begin
  107. Result := TJLang_Class.JavaClass.forName(StringToJString(APackageClassName), True, TAndroidHelper.Context.getClassLoader);
  108. end;
  109. class function TAndroidHelperEx.GetDefaultIconID: Integer;
  110. begin
  111. Result := TAndroidHelper.Context.getApplicationInfo.icon;
  112. end;
  113. class function TAndroidHelperEx.GetDefaultNotificationSound: Jnet_Uri;
  114. begin
  115. Result := TJRingtoneManager.JavaClass.getDefaultUri(TJRingtoneManager.JavaClass.TYPE_NOTIFICATION);
  116. end;
  117. class function TAndroidHelperEx.GetJActivity: JActivity;
  118. begin
  119. Result :=
  120. {$IF CompilerVersion > 27}
  121. TAndroidHelper.Activity
  122. {$ELSE}
  123. SharedActivity
  124. {$ENDIF}
  125. end;
  126. class function TAndroidHelperEx.GetJContext: JContext;
  127. begin
  128. Result :=
  129. {$IF CompilerVersion > 27}
  130. TAndroidHelper.Context;
  131. {$ELSE}
  132. SharedActivityContext;
  133. {$ENDIF}
  134. end;
  135. class function TAndroidHelperEx.GetPackageName: string;
  136. begin
  137. Result := JStringToString(Context.getPackageName);
  138. end;
  139. class function TAndroidHelperEx.GetTargetSdkVersion: Integer;
  140. var
  141. LApplicationInfo: JApplicationInfo;
  142. begin
  143. LApplicationInfo := TAndroidHelper.Context.getPackageManager.getApplicationInfo(TAndroidHelper.Context.getPackageName, 0);
  144. Result := LApplicationInfo.targetSdkVersion;
  145. end;
  146. class function TAndroidHelperEx.IsActivityForeground: Boolean;
  147. var
  148. LService: JObject;
  149. LRunningApps: JList;
  150. LAppInfo: JActivityManager_RunningAppProcessInfo;
  151. I: Integer;
  152. begin
  153. Result := False;
  154. LService := TAndroidHelper.Context.getSystemService(TJContext.JavaClass.ACTIVITY_SERVICE);
  155. LRunningApps := TJActivityManager.Wrap(TAndroidHelper.JObjectToID(LService)).getRunningAppProcesses;
  156. for I := 0 to LRunningApps.size - 1 do
  157. begin
  158. LAppInfo := TJActivityManager_RunningAppProcessInfo.Wrap(TAndroidHelper.JObjectToID(LRunningApps.get(I)));
  159. if LAppInfo.importance = 100 then
  160. begin
  161. if LAppInfo.importanceReasonComponent <> nil then
  162. begin
  163. if LAppInfo.importanceReasonComponent.getPackageName.equals(TAndroidHelper.Context.getPackageName) then
  164. Exit(True);
  165. end
  166. else if LRunningApps.size = 1 then
  167. Exit(True);
  168. end;
  169. end;
  170. end;
  171. class function TAndroidHelperEx.UriFromFile(const AFile: JFile): Jnet_Uri;
  172. var
  173. LAuthority: JString;
  174. begin
  175. if CheckBuildAndTarget(NOUGAT) then
  176. begin
  177. LAuthority := StringToJString(JStringToString(TAndroidHelper.Context.getApplicationContext.getPackageName) + '.fileprovider');
  178. Result := TJFileProvider.JavaClass.getUriForFile(TAndroidHelper.Context, LAuthority, AFile);
  179. end
  180. else
  181. Result := TJnet_uri.JavaClass.fromFile(AFile);
  182. end;
  183. class function TAndroidHelperEx.UriFromFileName(const AFileName: string): Jnet_Uri;
  184. begin
  185. Result := UriFromFile(TJFile.JavaClass.init(StringToJString(AFileName)));
  186. end;
  187. end.