FlyFilesUtils.pas 106 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600
  1. unit FlyFilesUtils;
  2. (* ************************************************ *)
  3. (*                         *)
  4. (*  设计:爱吃猪头肉 & Flying Wang 2013-11-30   *)
  5. (*      上面的版权声明请不要移除。      *)
  6. (*      Ver 1.0.2014.808           *)
  7. (*                         *)
  8. (* ************************************************ *)
  9. //1.0.2014.1108
  10. //支持 UTF8 的检查。
  11. //1.0.2014.908
  12. //支持 XE7。
  13. //1.0.2014.808
  14. //增加函数 IsPadOrPC。
  15. //1.0.2014.805
  16. //增加安卓下的获取 内存 SD 卡空间的函数。
  17. //1.0.2014.419
  18. //增加对 XE6 的支持。
  19. //1.0.2014.225
  20. //增加对 JNI 的接口查找功能。不完善。
  21. //1.0.2013.1219
  22. //在 亚瑟(Arthur) 3140223 的启发下,增加了更多的 SD 目录。
  23. //1.0.2013.1217
  24. //增加一个 FindSDCardSubPath 函数,用于查找指定的目录。
  25. //1.0.2013.1206
  26. //增加 [佛山]N.E(1024317) 9:36:38 提供的几个 SD 卡的路径。
  27. interface
  28. uses System.SysUtils,
  29. System.Classes,
  30. {$IFDEF ANDROID}
  31. Androidapi.JNIBridge,
  32. Androidapi.IOUtils,
  33. {$ENDIF ANDROID}
  34. {$IFDEF MSWINDOWS}
  35. Winapi.Windows,
  36. {$ENDIF MSWINDOWS}
  37. {$IFDEF POSIX}
  38. Posix.Dlfcn, Posix.Fcntl, Posix.SysStat, Posix.SysTime, Posix.SysTypes, Posix.Locale,
  39. {$ENDIF POSIX}
  40. {$IFDEF PC_MAPPED_EXCEPTIONS}
  41. System.Internal.Unwinder,
  42. {$ENDIF PC_MAPPED_EXCEPTIONS}
  43. {$IFDEF MACOS}
  44. Macapi.Mach, Macapi.CoreServices, Macapi.CoreFoundation,
  45. {$ENDIF MACOS}
  46. System.SysConst,
  47. System.IOUtils;
  48. var
  49. Error_NotFoundFileManager_Str: string = 'Not Found FileManager.';
  50. /// <summary>
  51. /// 返回大小写敏感的文件或路径名称
  52. /// </summary>
  53. /// <param name="FileName">
  54. /// 文件或路径名
  55. /// </param>
  56. /// <param name="RootPath">
  57. /// <para>
  58. /// 检查路径的根目录
  59. /// </para>
  60. /// <para>
  61. /// 当大小写检查到此目录时停止,不再继续检查。
  62. /// </para>
  63. /// </param>
  64. function GetCaseSensitiveFileName(const FileName: string; RootPath: string = ''): string;
  65. const
  66. /// <summary>
  67. /// 外置设备的数量
  68. /// </summary>
  69. OTGDeivceCount = 16;
  70. /// <summary>
  71. /// USB 磁盘,例如 U 盘、移动硬盘等
  72. /// </summary>
  73. UsbDiskStartIndex = 255;
  74. /// <summary>
  75. /// 外置光驱
  76. /// </summary>
  77. CDROMStartIndex = 255 + OTGDeivceCount;
  78. const
  79. /// <summary>
  80. /// 默认的删除等待时间,单位微秒
  81. /// </summary>
  82. DeleteDirectories_WaitMinSecond = 2000;
  83. /// <summary>
  84. /// 检查 SD 卡或路径是否可用
  85. /// </summary>
  86. function isPathCanUseNow(const PathOrDir: string; const Default: Boolean = True): Boolean;
  87. /// <summary>
  88. /// 检查 SD 卡或路径是否写入
  89. /// </summary>
  90. function TestPathCanWrite(const PathOrDir: string): Boolean;
  91. /// <summary>
  92. /// 获取 手机存储 或 SD 卡的路径
  93. /// </summary>
  94. /// <param name="Index">
  95. /// 0 为 手机存储 1 为 SD 卡
  96. /// </param>
  97. /// <returns>
  98. /// <para>
  99. /// 如果找到,返回路径。带 / 或 \
  100. /// </para>
  101. /// <para>
  102. /// 没找到,返回一个错误的路径。
  103. /// </para>
  104. /// </returns>
  105. function GetSDCardPath(Index: Integer = 0): string;
  106. /// <summary>
  107. /// 查找 手机存储 或 SD 卡上的某个路径
  108. /// </summary>
  109. /// <param name="SubPath">
  110. /// 被查找的子路径
  111. /// </param>
  112. /// <param name="Index">
  113. /// 0 为 手机存储 1 为 SD 卡
  114. /// </param>
  115. /// <returns>
  116. /// 如果找到,返回路径。带 / 或 \ 没找到,返回一个错误的路径。
  117. /// </returns>
  118. function FindSDCardSubPath(SubPath: string; Index: Integer = 0): string;
  119. /// <summary>
  120. /// 获取当成工程的运行路径
  121. /// </summary>
  122. function GetAppPath: string;
  123. /// <summary>
  124. /// 查找一个路径下的指定格式的文件
  125. /// </summary>
  126. /// <param name="Path">
  127. /// 路径,必须用通配符结束。例如 /*
  128. /// </param>
  129. /// <param name="Attr">
  130. /// 需要查找的文件的属性
  131. /// </param>
  132. /// <param name="List">
  133. /// 返回一个文件名或目录名的列表
  134. /// </param>
  135. /// <param name="JustFile">
  136. /// 是否只查找文件
  137. /// </param>
  138. /// <returns>
  139. /// 无意义
  140. /// </returns>
  141. function BuildFileListInAPath(const Path: string; const Attr: Integer; const List: TStrings;
  142. JustFile: Boolean = False): Boolean; overload;
  143. /// <summary>
  144. /// 查找一个路径下的指定格式的文件
  145. /// </summary>
  146. /// <param name="Path">
  147. /// 路径,必须用通配符结束。例如 /*
  148. /// </param>
  149. /// <param name="Attr">
  150. /// 需要查找的文件的属性
  151. /// </param>
  152. /// <param name="JustFile">
  153. /// 是否只查找文件
  154. /// </param>
  155. /// <returns>
  156. /// 返回换行分割的文件名或目录的列表
  157. /// </returns>
  158. function BuildFileListInAPath(const Path: string; const Attr: Integer;
  159. JustFile: Boolean = False): string; overload;
  160. /// <summary>
  161. /// 查找指定路径下的文件
  162. /// </summary>
  163. /// <param name="DirName">
  164. /// 路径
  165. /// </param>
  166. /// <param name="SearchFilter">
  167. /// 通配符组成的查找格式
  168. /// </param>
  169. /// <param name="FileAttribs">
  170. /// 需要查找的文件的属性
  171. /// </param>
  172. /// <param name="isIncludeSubDirName">
  173. /// 是否包含子目录的名字
  174. /// </param>
  175. /// <param name="Recursion">
  176. /// 是否递归找子目录
  177. /// </param>
  178. /// <param name="FullName">
  179. /// 是否返回完整路径
  180. /// </param>
  181. /// <returns>
  182. /// 返回换行分割的文件名或目录的列表
  183. /// </returns>
  184. function GetFileNamesFromDirectory(const DirName: string; const SearchFilter: string = '*';
  185. const FileAttribs: Integer = faAnyFile; const isIncludeSubDirName: Boolean = False; const Recursion: Boolean = False;
  186. const FullName: Boolean = False): string;
  187. //可以用 TDirectory.Delete 代替下面的功能。
  188. /// <summary>
  189. /// 删除目录下指定的文件
  190. /// </summary>
  191. /// <param name="Source">
  192. /// 被删除的文件路径,可以使用通配符
  193. /// </param>
  194. /// <param name="AbortOnFailure">
  195. /// 失败时是否退出
  196. /// </param>
  197. /// <param name="YesToAll">
  198. /// 删掉所有文件,包括只读的。仅 WIN32 下有效。
  199. /// </param>
  200. /// <param name="WaitMinSecond">
  201. /// 检查文件删除的等待时间,单位 微秒
  202. /// </param>
  203. /// <returns>
  204. /// 是否删除完成
  205. /// </returns>
  206. function DeleteDirectoryByEcho(const Source: string;
  207. AbortOnFailure: Boolean = False; YesToAll: Boolean = True;
  208. WaitMinSecond: Integer = DeleteDirectories_WaitMinSecond): Boolean;
  209. /// <summary>
  210. /// 获取指定路径的总存储大小
  211. /// </summary>
  212. function GetTotalSpaceSize(Path: string = PathDelim): UInt64;
  213. /// <summary>
  214. /// 获取指定路径的可以使用的存储大小
  215. /// </summary>
  216. function GetAvailableSpaceSize(Path: string = PathDelim): UInt64;
  217. /// <summary>
  218. /// 获取指定路径的剩余(包括不可使用的)存储大小
  219. /// </summary>
  220. function GetFreeSpaceSize(Path: string = PathDelim): UInt64;
  221. /// <summary>
  222. /// 获取总内存大小
  223. {$IFDEF ANDROID}
  224. /// 感谢[上海]故国(370620516)
  225. {$ENDIF}
  226. /// </summary>
  227. function GetTotalMemorySize: UInt64;
  228. /// <summary>
  229. /// 获取剩余内存大小
  230. {$IFDEF ANDROID}
  231. /// 感谢[上海]故国(370620516)
  232. {$ENDIF}
  233. /// </summary>
  234. function GetFreeMemorySize: UInt64;
  235. /// <summary>
  236. /// 安卓 IOS 返回是否是 PAD(平板)
  237. /// 其他平台,返回 True
  238. /// 很多手机的 DPI 是错的,所以获取的尺寸也就不正常了,
  239. /// 所以个别手机会被识别成 PAD。
  240. /// </summary>
  241. function IsPadOrPC: Boolean;
  242. //function IsPadOrPC(MiniScreenInches: Single = 6.2): Boolean;
  243. //function IsPad: Boolean;
  244. /// <summary>
  245. /// 在其他 APP 中打开文件。
  246. /// </summary>
  247. function OpenFileOnExtApp(const FileName: string; Https: Boolean = True): Boolean;
  248. function NowGMT_UTC: TDateTime;
  249. /// <summary>
  250. /// 获取完整 URL 的 Encode 结果。
  251. /// Just UTF8
  252. /// </summary>
  253. function EncodeURLWithSchemeOrProtocol(const URL: string): string;
  254. //上面是跨平台函数。
  255. //下面是平台函数。
  256. {$IFDEF ANDROID}
  257. function GetVolumePaths: string;
  258. function GetExternalStoragePath: string;
  259. //var
  260. // ExterStoragePathCanRead: Boolean = True;
  261. // ExterStoragePathCanWrite: Boolean = True;
  262. // SDMountedMessageReceived: Boolean = False;
  263. function GetExterStoragePath: string;
  264. function GetInnerStoragePath: string;
  265. /// <summary>
  266. /// It check SDCard0 Removable
  267. /// </summary>
  268. function GetIsExternalStorageRemovable: Boolean;
  269. /// <summary>
  270. /// 很多手机的 DPI 是错的,所以获取的尺寸也就不正常了。
  271. /// </summary>
  272. function GetScreenClientInches: Single;
  273. /// <summary>
  274. /// 获取安卓下剩余内存大小
  275. /// </summary>
  276. //function GetActiveMemorySize: UInt64;
  277. /// <summary>
  278. /// 查找一个 JAVA 类是否可以使用
  279. /// </summary>
  280. /// <param name="NamePath">
  281. /// 类的全路径
  282. /// </param>
  283. function IsCanFindJavaClass(const NamePath: string): Boolean;
  284. function IsCanFindJavaMethod(const MethodName, Signature: string; const CalssNamePath: string = ''): Boolean;
  285. function IsCanFindJavaStaticMethod(const MethodName, Signature: string; const CalssNamePath: string = ''): Boolean;
  286. type
  287. TGetFileNameListener = reference to procedure(const IsOK: Boolean; const FileName:string);
  288. TGetFileNameLIsternerMethod = procedure (const IsOK: Boolean; const FileName:string) of object;
  289. function OpenFileDialog(Title, FileExtension:string; GetFileNameCallBack: TGetFileNameListener): Boolean; overload;
  290. function OpenFileDialog(Title, FileExtension:string; GetFileNameCallBack: TGetFileNameLIsternerMethod): Boolean; overload;
  291. function CheckPermission(const APermissionName: string): Boolean;
  292. const
  293. C_android_permission_EXTERNAL_STORAGE = 'android.permission.WRITE_EXTERNAL_STORAGE';
  294. // C_android_permission_WRITE_MEDIA = 'android.permission.WRITE_MEDIA_STORAGE';
  295. function CanWriteExterStorage: Boolean;
  296. /// <summary>
  297. /// 更新相册
  298. /// </summary>
  299. procedure UpdateAlbum(FileNames: string);
  300. function ReadNoSizeFileToString(const AFileName: string): string;
  301. function ReadFileToString(const AFileName: string): string;
  302. {$ENDIF}
  303. implementation
  304. uses
  305. {$IFDEF ANDROID}
  306. {$IF CompilerVersion >= 27.0} // >= XE6
  307. Androidapi.Helpers,
  308. // FMX.Helpers.Android,
  309. {$ENDIF}
  310. {$IF CompilerVersion < 28.0} // < XE7
  311. FMX.Helpers.Android,
  312. {$ENDIF}
  313. Androidapi.Jni,
  314. Androidapi.JNI.Environment,
  315. Androidapi.JNI.StatFs,
  316. Androidapi.JNI.Stream2,
  317. Androidapi.JNI.ActivityManager,
  318. Androidapi.JNI.JavaTypes,
  319. Androidapi.NativeActivity,
  320. Androidapi.JNI.GraphicsContentViewText,
  321. Androidapi.JNI.Util,
  322. Androidapi.JNI.android.os.storage.StorageManager,
  323. Androidapi.JNI.java.lang.FlyUtils,
  324. Androidapi.JNI.Webkit,
  325. // Androidapi.JNI.Embarcadero,
  326. Androidapi.JNI.App,
  327. Androidapi.JNI.Net,
  328. Androidapi.JNI.Media,
  329. Androidapi.JNI.Provider,
  330. {$ENDIF}
  331. {$IF DEFINED(IOS) or DEFINED(MACOS)}
  332. iOSapi.Foundation,
  333. Macapi.ObjectiveC,
  334. FMX.Helpers.iOS,
  335. // iOSapi.UIDevice2,
  336. {$ENDIF}
  337. {$IFDEF MSWINDOWS}
  338. Winapi.ShellApi,
  339. {$ENDIF}
  340. // FMX.Dialogs,
  341. {$IF CompilerVersion >= 29.0} // XE8
  342. System.NetEncoding,
  343. {$ELSE}
  344. IdURI,
  345. {$ENDIF}
  346. System.Rtti,
  347. System.TypInfo,
  348. System.Messaging,
  349. System.Math;
  350. //来自 inc 内部的 类型。
  351. {$IF DEFINED(IOS) or DEFINED(MACOS)}
  352. type
  353. { Used by other time functions. }
  354. tm = record
  355. tm_sec: Integer; // Seconds. [0-60] (1 leap second)
  356. tm_min: Integer; // Minutes. [0-59]
  357. tm_hour: Integer; // Hours.[0-23]
  358. tm_mday: Integer; // Day.[1-31]
  359. tm_mon: Integer; // Month.[0-11]
  360. tm_year: Integer; // Year since 1900
  361. tm_wday: Integer; // Day of week [0-6] (Sunday = 0)
  362. tm_yday: Integer; // Days of year [0-365]
  363. tm_isdst: Integer; // Daylight Savings flag [-1/0/1]
  364. tm_gmtoff: LongInt; // Seconds east of UTC
  365. tm_zone: MarshaledAString; // Timezone abbreviation
  366. end;
  367. {$EXTERNALSYM tm}
  368. Ptm = ^tm;
  369. {$ELSEIF DEFINED(ANDROID)}
  370. { Used by other time functions. }
  371. type
  372. tm = record
  373. tm_sec: Integer; // Seconds. [0-60] (1 leap second)
  374. tm_min: Integer; // Minutes. [0-59]
  375. tm_hour: Integer; // Hours.[0-23]
  376. tm_mday: Integer; // Day.[1-31]
  377. tm_mon: Integer; // Month.[0-11]
  378. tm_year: Integer; // Year since 1900
  379. tm_wday: Integer; // Day of week [0-6] (Sunday = 0)
  380. tm_yday: Integer; // Days of year [0-365]
  381. tm_isdst: Integer; // Daylight Savings flag [-1/0/1]
  382. tm_gmtoff: LongInt; // Seconds east of UTC
  383. tm_zone: MarshaledAString; // Timezone abbreviation
  384. end;
  385. {$EXTERNALSYM tm}
  386. Ptm = ^tm;
  387. {$ENDIF}
  388. //来自 inc 的函数定义。
  389. {$IFDEF POSIX}
  390. const
  391. {$IFDEF UNDERSCOREIMPORTNAME}
  392. _PU = '_';
  393. {$ELSE}
  394. _PU = '';
  395. {$ENDIF}
  396. const
  397. libc = '/usr/lib/libc.dylib';
  398. libpthread = '/usr/lib/libpthread.dylib';
  399. libiconv = '/usr/lib/libiconv.dylib';
  400. libdl = '/usr/lib/libdl.dylib';
  401. {$IF not Declared(_PU)}
  402. const
  403. // On Mac OSX, cdecl names have a preceeding underscore
  404. // if x86 native backend.
  405. {$IF Defined(UNDERSCOREIMPORTNAME)}
  406. _PU = '_';
  407. {$ELSE}
  408. _PU = '';
  409. {$ENDIF}
  410. {$EXTERNALSYM _PU}
  411. {$ENDIF}
  412. const
  413. {$IFNDEF IOS}
  414. _INODE_SUFFIX = '$INODE64';
  415. {$ELSE IOS}
  416. _INODE_SUFFIX = '';
  417. {$ENDIF !IOS}
  418. {$EXTERNALSYM _INODE_SUFFIX}
  419. //具体函数定义开始。
  420. function _system(Name: MarshaledAString): Integer; cdecl;
  421. external libc name _PU + 'system';
  422. function tempnam(const Path: MarshaledAString; const Prefix: MarshaledAString): MarshaledAString; cdecl;
  423. external libc name _PU + 'tempnam';
  424. {$EXTERNALSYM tempnam}
  425. procedure free(p: Pointer); cdecl;
  426. external libc name _PU + 'free';
  427. {$EXTERNALSYM free}
  428. function gettimeofday(var timeval: timeval; timezone: Pointer): Integer; cdecl;
  429. external libc name _PU + 'gettimeofday';
  430. {$EXTERNALSYM gettimeofday}
  431. function gmtime_r(var Timer: time_t; var UnixTime: tm): Ptm; cdecl;
  432. external libc name _PU + 'gmtime_r';
  433. {$EXTERNALSYM gmtime_r}
  434. {$ENDIF}
  435. //可以开始写函数了。
  436. function NowGMT_UTC: TDateTime;
  437. {$IFDEF MSWINDOWS}
  438. var
  439. SystemTime: TSystemTime;
  440. begin
  441. GetSystemTime(SystemTime);
  442. Result := EncodeDate(SystemTime.wYear, SystemTime.wMonth, SystemTime.wDay) +
  443. EncodeTime(SystemTime.wHour, SystemTime.wMinute, SystemTime.wSecond, SystemTime.wMilliseconds);
  444. end;
  445. {$ENDIF MSWINDOWS}
  446. {$IFDEF POSIX}
  447. var
  448. T: time_t;
  449. TV: timeval;
  450. UT: tm;
  451. begin
  452. gettimeofday(TV, nil);
  453. T := TV.tv_sec;
  454. gmtime_r(T, UT);
  455. Result := EncodeDate(UT.tm_year + 1900, UT.tm_mon + 1, UT.tm_mday) +
  456. EncodeTime(UT.tm_hour, UT.tm_min, UT.tm_sec, TV.tv_usec div 1000);
  457. end;
  458. {$ENDIF POSIX}
  459. function EncodeURLWithSchemeOrProtocol(const URL: string): string;
  460. var
  461. Protocol: string;
  462. AURL: string;
  463. AIndex: Integer;
  464. begin
  465. {$IF CompilerVersion >= 29.0} // XE8
  466. AURL := URL.Trim;
  467. Protocol := '';
  468. AIndex := AURL.IndexOf('//');
  469. if AIndex > 0 then
  470. begin
  471. Protocol := AURL.Substring(0, AIndex + 1); // has /
  472. AURL := AURL.Substring(AIndex + 1); // has /
  473. end;
  474. Result := Protocol + TNetEncoding.URL.EncodePath(AURL);
  475. {$ELSE}
  476. Result := TIdURI.URLEncode(Result);
  477. {$ENDIF}
  478. end;
  479. function EncodeURLWithOutSchemeOrProtocol(const URL: string; Https: Boolean = True): string;
  480. begin
  481. Result := URL;
  482. if FileExists(Result) then
  483. begin
  484. {$IFDEF MSWINDOWS}
  485. {$ELSE}
  486. if Result.Substring(0, 1) <> '/' then
  487. begin
  488. Result := '/' + Result;
  489. end;
  490. Result := 'file://' + Result;
  491. {$ENDIF}
  492. end
  493. else
  494. begin
  495. if Https then
  496. begin
  497. Result := 'https://' + URL;
  498. end
  499. else
  500. begin
  501. Result := 'http://' + URL;
  502. end;
  503. Result := EncodeURLWithSchemeOrProtocol(Result);
  504. end;
  505. end;
  506. function OpenFileOnExtApp(const FileName: string; Https: Boolean = True): Boolean;
  507. {$IFDEF ANDROID}
  508. var
  509. Intent: JIntent;
  510. FileExtension: string;
  511. mime: JMimeTypeMap;
  512. MIMEType: JString;
  513. TempStr,
  514. FileToOpen: string;
  515. AJFile: JFile;
  516. AJUri: Jnet_Uri;
  517. begin
  518. // There may be an issue with the geo: prefix and URLEncode.
  519. // will need to research
  520. Result := False;
  521. if FileName = '' then
  522. Exit;
  523. FileExtension := AnsiLowerCase(ExtractFileExt(FileName));
  524. if FileExtension = '' then
  525. Exit;
  526. mime := TJMimeTypeMap.JavaClass.getSingleton();
  527. MIMEType := nil;
  528. if mime <> nil then
  529. begin
  530. MIMEType := mime.getMimeTypeFromExtension(StringToJString(FileExtension.Substring(1)));
  531. end;
  532. if MIMEType <> nil then
  533. begin
  534. // 调用相应程序打开当前程序
  535. Intent := TJIntent.Create;
  536. Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
  537. //TempStr := IncludeTrailingPathDelimiter(TPath.GetDocumentsPath)
  538. TempStr := IncludeTrailingPathDelimiter(TPath.GetHomePath);
  539. {$IF CompilerVersion >= 33.0} // RAD10.3
  540. if FileExists(FileName) then
  541. begin
  542. FileToOpen := FileName;
  543. end
  544. else
  545. begin
  546. FileToOpen := EncodeURLWithOutSchemeOrProtocol(FileName, Https);
  547. end;
  548. if Pos(TempStr, FileToOpen) = 1 then
  549. begin
  550. AJFile := TJFile.JavaClass.init(StringToJString(FileToOpen));
  551. AJUri := TAndroidHelper.JFileToJURI(AJFile);
  552. end
  553. else
  554. begin
  555. {$ELSE}
  556. begin
  557. {$ENDIF}
  558. FileToOpen := EncodeURLWithOutSchemeOrProtocol(FileName, Https);
  559. AJUri := StrToJURI(FileToOpen);
  560. end;
  561. Intent.setDataAndType(AJUri, MIMEType);
  562. try
  563. SharedActivity.startActivity(Intent);
  564. Result := True;
  565. except
  566. end;
  567. end;
  568. end;
  569. {$ELSE}
  570. {$IFDEF IOS}
  571. var
  572. NSU: NSUrl;
  573. AURL: string;
  574. begin
  575. Result := False;
  576. AURL := EncodeURLWithOutSchemeOrProtocol(FileName, Https);
  577. // iOS doesn't like spaces, so URL encode is important.
  578. NSU := StrToNSUrl(AURL);
  579. if TOSVersion.Check(9) or SharedApplication.canOpenURL(NSU) then
  580. try
  581. Result := SharedApplication.openUrl(NSU);
  582. except
  583. end;
  584. end;
  585. {$ELSE}
  586. {$IFDEF MSWINDOWS}
  587. var
  588. AURL: string;
  589. begin
  590. Result := False;
  591. AURL := EncodeURLWithOutSchemeOrProtocol(FileName, Https);
  592. try
  593. ShellExecute(GetActiveWindow, 'open', PChar(AURL), '', '', SW_MAXIMIZE);
  594. Result := True;
  595. except
  596. end;
  597. end;
  598. {$ELSE}
  599. var
  600. M:TMarshaller;
  601. AURL: string;
  602. begin
  603. Result := False;
  604. //AURL := 'open -a Safari ' + EncodeURLWithOutSchemeOrProtocol(AURL);
  605. AURL := 'open ' + EncodeURLWithOutSchemeOrProtocol(FileName, Https);
  606. try
  607. _system(M.AsAnsi(AURL, CP_UTF8).ToPointer);
  608. Result := True;
  609. except
  610. end;
  611. // raise Exception.Create('Not supported!');
  612. end;
  613. {$ENDIF MSWINDOWS}
  614. {$ENDIF IOS}
  615. {$ENDIF ANDROID}
  616. function IsPadOrPC: Boolean;
  617. {$IF DEFINED(IOS) or DEFINED(MACOS)}
  618. {$IFDEF IOS}
  619. begin
  620. Result := IsPad;
  621. end;
  622. {$ELSE IOS}
  623. begin
  624. Result := True;
  625. end;
  626. {$ENDIF IOS}
  627. {$ENDIF IOS or MACOS}
  628. {$IFDEF MSWINDOWS}
  629. begin
  630. Result := True;
  631. end;
  632. {$ENDIF}
  633. {$IFDEF ANDROID}
  634. //var
  635. // ScreenInches2,
  636. // ScreenInches1,
  637. // x,y: Double;
  638. // dm: JDisplayMetrics;
  639. //begin
  640. // Result := False;
  641. // dm := GetJDisplayMetrics;
  642. // if dm = nil then exit;
  643. // x := dm.widthPixels;
  644. // y := dm.heightPixels;
  645. // try
  646. // ScreenInches1 := Sqrt((x * x / dm.xdpi / dm.xdpi) + (y * y / dm.ydpi / dm.ydpi));
  647. // ScreenInches2 := Sqr(x * x + y * Y ) / dm.densityDpi;
  648. // except
  649. // exit;
  650. // end;
  651. // Result := ScreenInches1 >= MiniScreenInches;
  652. // if Result then
  653. // Result := ScreenInches2 >= MiniScreenInches;
  654. //end;
  655. var
  656. IsTablet: Boolean;
  657. begin
  658. Result := False;
  659. IsTablet := False;
  660. // CallInUIThreadAndWaitFinishing(
  661. // procedure
  662. // begin
  663. IsTablet := SharedActivity.getResources.getConfiguration.screenLayout and
  664. TJConfiguration.JavaClass.SCREENLAYOUT_SIZE_MASK >= TJConfiguration.JavaClass.SCREENLAYOUT_SIZE_LARGE;
  665. // end);
  666. Result := IsTablet;
  667. end;
  668. {$ENDIF}
  669. function IsPad: Boolean;
  670. begin
  671. {$IFDEF MSWINDOWS}
  672. begin
  673. //code by [龟山]Aone(1467948783)
  674. Result := TOSVersion.Check(6, 1) and
  675. (GetSystemMetrics(SM_TABLETPC) <> 0) and
  676. ((GetSystemMetrics(SM_DIGITIZER) and NID_MULTI_INPUT) = NID_MULTI_INPUT);
  677. end;
  678. {$ELSE MSWINDOWS}
  679. {$IF DEFINED(IOS) or DEFINED(MACOS)}
  680. {$IFDEF IOS}
  681. begin
  682. Result := IsPad;
  683. end;
  684. {$ELSE IOS}
  685. begin
  686. Result := False;
  687. end;
  688. {$ENDIF IOS}
  689. {$ELSE} //IF DEFINED(IOS) or DEFINED(MACOS)
  690. Result := IsPadOrPC;
  691. {$ENDIF IOS or MACOS}
  692. {$ENDIF MSWINDOWS}
  693. end;
  694. function GetTotalMemorySize: UInt64;
  695. {$IF DEFINED(IOS) or DEFINED(MACOS)}
  696. begin
  697. // Result := TUIDevice2.Wrap(TUIDevice2.OCClass.currentDevice).totalMemory;
  698. Result := NSRealMemoryAvailable;
  699. end;
  700. {$ENDIF}
  701. {$IFDEF MSWINDOWS}
  702. var
  703. lpBuffer : TMEMORYSTATUSEX;
  704. begin
  705. Result := 0;
  706. ZeroMemory(@lpBuffer, Sizeof(TMEMORYSTATUSEX));
  707. lpBuffer.dwLength := Sizeof(TMEMORYSTATUSEX);
  708. GlobalMemoryStatusEx(lpBuffer);
  709. Result := lpBuffer.ullTotalPhys;
  710. end;
  711. {$ENDIF}
  712. {$IFDEF ANDROID}
  713. var
  714. Mgr: JActivityManager;
  715. MgrNative: JObject;
  716. MemInfo: JActivityManager_MemoryInfo;
  717. AStrings: TStringList;
  718. TempValue: string;
  719. AReader: JReader;
  720. ABufferedReader: JBufferedReader;
  721. begin
  722. Result := 0;
  723. MgrNative :=
  724. {$IF CompilerVersion >= 30.0} // >=RAD10
  725. TAndroidHelper.Context
  726. {$ELSE}
  727. SharedActivityContext
  728. {$ENDIF}
  729. .getSystemService(TJContext.JavaClass.ACTIVITY_SERVICE);
  730. if MgrNative <> nil then
  731. begin
  732. Mgr := TJActivityManager.Wrap((MgrNative as ILocalObject).GetObjectID);
  733. MemInfo := TJActivityManager_MemoryInfo.JavaClass.init;
  734. Mgr.getMemoryInfo(MemInfo);
  735. try
  736. Result := UInt64(MemInfo.totalMem);
  737. except
  738. //API level < 16
  739. try
  740. Result := UInt64(MemInfo.availMem);
  741. except
  742. Result := 0;
  743. end;
  744. if FileExists('/proc/meminfo') then
  745. begin
  746. AStrings := TStringList.Create;
  747. try
  748. AStrings.LineBreak := sLineBreak;
  749. AStrings.NameValueSeparator := ':';
  750. AStrings.Clear;
  751. AReader := TJFileReader.JavaClass.init(StringToJString('/proc/meminfo')) as JReader;
  752. ABufferedReader := TJBufferedReader.JavaClass.init(AReader);
  753. repeat
  754. TempValue := JStringToString(ABufferedReader.readLine);
  755. if TempValue <> '' then
  756. begin
  757. AStrings.Add(TempValue);
  758. end;
  759. until (not ABufferedReader.ready);
  760. ABufferedReader.close;
  761. TempValue := AStrings.Values['MemTotal'].Trim;
  762. AStrings.Clear;
  763. AStrings.NameValueSeparator := ' ';
  764. AStrings.Add(TempValue.Trim);
  765. TempValue := AStrings.Names[0];
  766. // ShowMessage(TempValue);
  767. Result := StrToInt64Def(TempValue, Result div 1024) * 1024;
  768. finally
  769. FreeAndNil(AStrings);
  770. end;
  771. end;
  772. end;
  773. end;
  774. end;
  775. {$ENDIF}
  776. function GetFreeMemorySize: UInt64;
  777. {$IF DEFINED(IOS) or DEFINED(MACOS)}
  778. begin
  779. //Result := Max(0, GetTotalMemorySize -
  780. // TUIDevice2.Wrap(TUIDevice2.OCClass.currentDevice).userMemory);
  781. Result := NSRealMemoryAvailable;
  782. end;
  783. {$ENDIF}
  784. {$IFDEF MSWINDOWS}
  785. var
  786. lpBuffer : TMEMORYSTATUSEX;
  787. begin
  788. Result := 0;
  789. ZeroMemory(@lpBuffer, Sizeof(TMEMORYSTATUSEX));
  790. lpBuffer.dwLength := Sizeof(TMEMORYSTATUSEX);
  791. GlobalMemoryStatusEx(lpBuffer);
  792. Result := lpBuffer.ullAvailPhys;
  793. end;
  794. {$ENDIF}
  795. {$IFDEF ANDROID}
  796. var
  797. Mgr: JActivityManager;
  798. MgrNative: JObject;
  799. MemInfo: JActivityManager_MemoryInfo;
  800. begin
  801. Result := 0;
  802. MgrNative :=
  803. {$IF CompilerVersion >= 30.0} // >=RAD10
  804. TAndroidHelper.Context
  805. {$ELSE}
  806. SharedActivityContext
  807. {$ENDIF}
  808. .getSystemService(TJContext.JavaClass.ACTIVITY_SERVICE);
  809. if MgrNative <> nil then
  810. begin
  811. Mgr := TJActivityManager.Wrap((MgrNative as ILocalObject).GetObjectID);
  812. MemInfo := TJActivityManager_MemoryInfo.JavaClass.init;
  813. Mgr.getMemoryInfo(MemInfo);
  814. try
  815. Result := UInt64(MemInfo.availMem);
  816. except
  817. Result := 0;
  818. end;
  819. end;
  820. end;
  821. {$ENDIF}
  822. function GetTotalSpaceSize(Path: string = PathDelim): UInt64;
  823. {$IF DEFINED(IOS) or DEFINED(MACOS)}
  824. var
  825. Dict: NSDictionary;
  826. P: Pointer;
  827. const
  828. FoundationFwk: string = '/System/Library/Frameworks/Foundation.framework/Foundation';
  829. begin
  830. Result := 0;
  831. if DirectoryExists(Path) or FileExists(Path) then
  832. begin
  833. end
  834. else
  835. begin
  836. raise Exception.Create('Path " ' + Path + '" not found.');
  837. end;
  838. Dict := TNSFileManager.Wrap(TNSFileManager.OCClass.defaultManager).attributesOfFileSystemForPath(NSStr(Path), nil);
  839. if Dict = nil then
  840. Exit;
  841. P := Dict.objectForKey((CocoaNSStringConst(FoundationFwk, 'NSFileSystemSize') as ILocalObject).GetObjectID);
  842. if P <> nil then
  843. Result := TNSNumber.Wrap(P).unsignedLongLongValue;
  844. end;
  845. {$ENDIF}
  846. {$IFDEF MSWINDOWS}
  847. var
  848. lpFreeBytesAvailableToCaller,
  849. lpTotalNumberOfBytes,
  850. lpTotalNumberOfFreeBytes: Int64;
  851. begin
  852. Result := 0;
  853. if DirectoryExists(Path) or FileExists(Path) then
  854. begin
  855. end
  856. else
  857. begin
  858. raise Exception.Create('Path " ' + Path + '" not found.');
  859. end;
  860. lpTotalNumberOfBytes := MaxInt;
  861. if GetDiskFreeSpaceEx(pchar(ExtractFileDrive(Path)),
  862. lpFreeBytesAvailableToCaller,
  863. lpTotalNumberOfBytes, @lpTotalNumberOfFreeBytes) then
  864. begin
  865. Result := UInt64(lpTotalNumberOfBytes);
  866. end;
  867. end;
  868. {$ENDIF}
  869. {$IFDEF ANDROID}
  870. var
  871. AJFile: JFile;
  872. AJStatFs: JStatFs;
  873. begin
  874. Result := 0;
  875. if DirectoryExists(Path) or FileExists(Path) then
  876. begin
  877. end
  878. else
  879. begin
  880. raise Exception.Create('Path " ' + Path + '" not found.');
  881. end;
  882. AJFile := TJFile.JavaClass.init(StringToJString(Path));
  883. if AJFile = nil then exit;
  884. AJStatFs := TJStatFs.JavaClass.init(AJFile.getPath);
  885. if AJStatFs = nil then exit;
  886. Result := UInt64(AJStatFs.getBlockSize) * UInt64(AJStatFs.getBlockCount);
  887. end;
  888. {$ENDIF}
  889. function GetFreeSpaceSize(Path: string = PathDelim): UInt64;
  890. {$IF DEFINED(IOS) or DEFINED(MACOS)}
  891. var
  892. Dict: NSDictionary;
  893. P: Pointer;
  894. const
  895. FoundationFwk: string = '/System/Library/Frameworks/Foundation.framework/Foundation';
  896. begin
  897. Result := 0;
  898. if DirectoryExists(Path) or FileExists(Path) then
  899. begin
  900. end
  901. else
  902. begin
  903. raise Exception.Create('Path " ' + Path + '" not found.');
  904. end;
  905. Dict := TNSFileManager.Wrap(TNSFileManager.OCClass.defaultManager).attributesOfFileSystemForPath(NSStr(Path), nil);
  906. if Dict = nil then
  907. Exit;
  908. P := Dict.objectForKey((CocoaNSStringConst(FoundationFwk, 'NSFileSystemFreeSize') as ILocalObject).GetObjectID);
  909. if P <> nil then
  910. Result := TNSNumber.Wrap(P).unsignedLongLongValue;
  911. end;
  912. {$ENDIF}
  913. {$IFDEF MSWINDOWS}
  914. var
  915. lpFreeBytesAvailableToCaller,
  916. lpTotalNumberOfBytes,
  917. lpTotalNumberOfFreeBytes: Int64;
  918. begin
  919. Result := 0;
  920. if DirectoryExists(Path) or FileExists(Path) then
  921. begin
  922. end
  923. else
  924. begin
  925. raise Exception.Create('Path " ' + Path + '" not found.');
  926. end;
  927. lpTotalNumberOfFreeBytes := MaxInt;
  928. if GetDiskFreeSpaceEx(pchar(ExtractFileDrive(Path)),
  929. lpFreeBytesAvailableToCaller,
  930. lpTotalNumberOfBytes, @lpTotalNumberOfFreeBytes) then
  931. begin
  932. Result := UInt64(lpTotalNumberOfFreeBytes);
  933. end;
  934. end;
  935. {$ENDIF}
  936. {$IFDEF ANDROID}
  937. var
  938. AJFile: JFile;
  939. AJStatFs: JStatFs;
  940. begin
  941. Result := 0;
  942. if DirectoryExists(Path) or FileExists(Path) then
  943. begin
  944. end
  945. else
  946. begin
  947. raise Exception.Create('Path " ' + Path + '" not found.');
  948. end;
  949. AJFile := TJFile.JavaClass.init(StringToJString(Path));
  950. if AJFile = nil then exit;
  951. AJStatFs := TJStatFs.JavaClass.init(AJFile.getPath);
  952. if AJStatFs = nil then exit;
  953. Result := UInt64(AJStatFs.getBlockSize) * UInt64(AJStatFs.getFreeBlocks);
  954. end;
  955. {$ENDIF}
  956. function GetAvailableSpaceSize(Path: string = PathDelim): UInt64;
  957. {$IF DEFINED(IOS) or DEFINED(MACOS)}
  958. begin
  959. Result := GetFreeSpaceSize(Path);
  960. end;
  961. {$ENDIF}
  962. {$IFDEF MSWINDOWS}
  963. var
  964. lpFreeBytesAvailableToCaller,
  965. lpTotalNumberOfBytes,
  966. lpTotalNumberOfFreeBytes: Int64;
  967. begin
  968. Result := 0;
  969. if DirectoryExists(Path) or FileExists(Path) then
  970. begin
  971. end
  972. else
  973. begin
  974. raise Exception.Create('Path " ' + Path + '" not found.');
  975. end;
  976. lpFreeBytesAvailableToCaller := MaxInt;
  977. if GetDiskFreeSpaceEx(pchar(ExtractFileDrive(Path)),
  978. lpFreeBytesAvailableToCaller,
  979. lpTotalNumberOfBytes, @lpTotalNumberOfFreeBytes) then
  980. begin
  981. Result := UInt64(lpFreeBytesAvailableToCaller);
  982. end;
  983. end;
  984. {$ENDIF}
  985. {$IFDEF ANDROID}
  986. var
  987. AJFile: JFile;
  988. AJStatFs: JStatFs;
  989. begin
  990. Result := 0;
  991. if DirectoryExists(Path) or FileExists(Path) then
  992. begin
  993. end
  994. else
  995. begin
  996. raise Exception.Create('Path " ' + Path + '" not found.');
  997. end;
  998. AJFile := TJFile.JavaClass.init(StringToJString(Path));
  999. if AJFile = nil then exit;
  1000. AJStatFs := TJStatFs.JavaClass.init(AJFile.getPath);
  1001. if AJStatFs = nil then exit;
  1002. Result := UInt64(AJStatFs.getBlockSize) * UInt64(AJStatFs.getAvailableBlocks);
  1003. end;
  1004. {$ENDIF}
  1005. {$IFDEF ANDROID}
  1006. function CanWriteExterStorage: Boolean;
  1007. begin
  1008. Result := False;
  1009. Result := CheckPermission(C_android_permission_EXTERNAL_STORAGE);
  1010. // if Result and TOSVersion.Check(4) then
  1011. // begin
  1012. // // 没有效果,而且返回 总是 False
  1013. // Result := CheckPermission(C_android_permission_WRITE_MEDIA);
  1014. // end;
  1015. end;
  1016. //copy form FMX.AddressBook.Android;
  1017. function CheckPermission(const APermissionName: string): Boolean;
  1018. var
  1019. PackageName: JString;
  1020. begin
  1021. PackageName := TAndroidHelper.Context.getPackageName;
  1022. // if TOSVersion.Check(6) then
  1023. // begin
  1024. // Result := TAndroidHelper.Context.checkSelfPermission(StringToJString(APermissionName)) =
  1025. // TJPackageManager.JavaClass.PERMISSION_GRANTED;
  1026. // exit;
  1027. // end;
  1028. Result := TAndroidHelper.Context.getPackageManager.checkPermission(StringToJString(APermissionName), PackageName) =
  1029. TJPackageManager.JavaClass.PERMISSION_GRANTED;
  1030. end;
  1031. function isExternalStorageDocument(URI: Jnet_Uri): Boolean;
  1032. begin
  1033. Result := False;
  1034. if URI = nil then exit;
  1035. Result := URI.getAuthority.equals(StringToJString('com.android.externalstorage.documents'));
  1036. end;
  1037. function isDownloadsDocument(URI: Jnet_Uri): Boolean;
  1038. begin
  1039. Result := False;
  1040. if URI = nil then exit;
  1041. Result := URI.getAuthority.equals(StringToJString('com.android.providers.downloads.documents'));
  1042. end;
  1043. function isMediaDocument(URI: Jnet_Uri): Boolean;
  1044. begin
  1045. Result := False;
  1046. if URI = nil then exit;
  1047. Result := URI.getAuthority.equals(StringToJString('com.android.providers.media.documents'));
  1048. end;
  1049. const
  1050. FILE_SELECT_CODE = 0;
  1051. var
  1052. FMessageChooserID: Integer = 0;
  1053. FGetFileNameCallBack1: TGetFileNameListener;
  1054. FGetFileNameCallBack2: TGetFileNameLIsternerMethod;
  1055. procedure HandleActivityMessageforChooser(const Sender: TObject; const M: TMessage);
  1056. var
  1057. MediaDocument,
  1058. ExternalStorageDocument,
  1059. IsOK: Boolean;
  1060. DocType,
  1061. DocIDs,
  1062. FileScheme,
  1063. FileName:string;
  1064. URI: Jnet_Uri;
  1065. cursor: JCursor;
  1066. projection: TJavaObjectArray<JString>;
  1067. column_index: Integer;
  1068. DocIDInfos: TArray<string>;
  1069. TempDocIDStr: JString;
  1070. selection: JString;
  1071. selectionArgs: TJavaObjectArray<JString>;
  1072. begin
  1073. IsOK := False;
  1074. FileName := '';
  1075. IsOK := M is TMessageResultNotification;
  1076. if IsOK and (TMessageResultNotification(M).RequestCode = FILE_SELECT_CODE) then
  1077. begin
  1078. IsOK := (TMessageResultNotification(M).ResultCode = TJActivity.JavaClass.RESULT_OK);
  1079. end;
  1080. if IsOK then
  1081. begin
  1082. IsOK := TMessageResultNotification(M).Value <> nil;
  1083. end;
  1084. if IsOK then
  1085. begin
  1086. URI := TMessageResultNotification(M).Value.getData;
  1087. IsOK := URI <> nil;
  1088. end;
  1089. FileName := '';
  1090. if IsOK then
  1091. begin
  1092. FileScheme := JStringToString(URI.getScheme);
  1093. if TOSVersion.Check(4, 4) and TJDocumentsContract.JavaClass.isDocumentUri(
  1094. {$IF CompilerVersion >= 30.0} // >=RAD10
  1095. TAndroidHelper.Context
  1096. {$ELSE}
  1097. SharedActivityContext
  1098. {$ENDIF}
  1099. , URI) then
  1100. begin
  1101. //http://blog.csdn.net/u011200844/article/details/43703593
  1102. ExternalStorageDocument := False;
  1103. MediaDocument := False;
  1104. selection := nil;
  1105. selectionArgs := nil;
  1106. IsOK := False;
  1107. if isDownloadsDocument(URI) then
  1108. begin
  1109. TempDocIDStr := TJDocumentsContract.JavaClass.getDocumentId(URI);
  1110. URI := TJContentUris.JavaClass.withAppendedId(
  1111. TJnet_Uri.JavaClass.parse(StringToJString('content://downloads/public_downloads')),
  1112. TJLong.JavaClass.valueOf(TempDocIDStr).longValue);
  1113. //后面会继续处理。
  1114. end
  1115. else
  1116. begin
  1117. ExternalStorageDocument := isExternalStorageDocument(URI);
  1118. if not ExternalStorageDocument then
  1119. MediaDocument := isMediaDocument(URI);
  1120. end;
  1121. if ExternalStorageDocument or MediaDocument then
  1122. begin
  1123. DocIDs := JStringToString(TJDocumentsContract.JavaClass.getDocumentId(URI));
  1124. DocIDInfos := DocIDs.Split([':']);
  1125. if Length(DocIDInfos) > 1 then
  1126. begin
  1127. DocType := DocIDInfos[0];
  1128. if ExternalStorageDocument then
  1129. begin
  1130. if SameText(DocType, 'primary') then
  1131. begin
  1132. FileName := GetSDCardPath(0) + DocIDInfos[1];
  1133. IsOK := True;
  1134. end
  1135. else
  1136. begin
  1137. //后面会继续处理。
  1138. end;
  1139. end
  1140. else if MediaDocument then
  1141. begin
  1142. if SameText(DocType, 'image') then
  1143. begin
  1144. URI := TJImages_Media.JavaClass.EXTERNAL_CONTENT_URI;
  1145. IsOK := True;
  1146. end
  1147. else if SameText(DocType, 'video') then
  1148. begin
  1149. URI := TJVideo_Media.JavaClass.EXTERNAL_CONTENT_URI;
  1150. IsOK := True;
  1151. end
  1152. else if SameText(DocType, 'audio') then
  1153. begin
  1154. URI := TJAudio_Media.JavaClass.EXTERNAL_CONTENT_URI;
  1155. IsOK := True;
  1156. end;
  1157. if IsOK then
  1158. begin
  1159. selection := StringToJString('_id=?');
  1160. selectionArgs := TJavaObjectArray<JString>.Create(1);
  1161. selectionArgs.Items[0] := StringToJString(DocIDInfos[1]);
  1162. IsOK := False;
  1163. //后面会继续处理。
  1164. end;
  1165. end;
  1166. end;
  1167. end;
  1168. end
  1169. else
  1170. IsOK := SameText('file', FileScheme);
  1171. end
  1172. else exit;
  1173. if IsOK and FileName.IsEmpty then
  1174. begin
  1175. FileName := JStringToString(URI.getPath);
  1176. end
  1177. else if not IsOK then
  1178. begin
  1179. IsOK := SameText('content', FileScheme);
  1180. if IsOK then
  1181. begin
  1182. IsOK := False;
  1183. projection := TJavaObjectArray<JString>.Create(1);
  1184. projection.Items[0] := StringToJString('_data');
  1185. try
  1186. cursor :=
  1187. {$IF CompilerVersion >= 30.0} // >=RAD10
  1188. TAndroidHelper.Context
  1189. {$ELSE}
  1190. SharedActivityContext
  1191. {$ENDIF}
  1192. .getContentResolver().query(URI, projection, selection, selectionArgs, nil);
  1193. if (cursor <> nil) then
  1194. begin
  1195. column_index := cursor.getColumnIndexOrThrow(StringToJString('_data'));
  1196. if cursor.moveToFirst then
  1197. begin
  1198. FileName := JStringToString(cursor.getString(column_index));
  1199. IsOK := True;
  1200. end;
  1201. end;
  1202. except
  1203. IsOK := False;
  1204. end;
  1205. end;
  1206. // if not IsOK then
  1207. // begin
  1208. // FileName := JStringToString(URI.getPath);
  1209. // FileName := FileName.Trim;
  1210. // IsOK := not FileName.IsEmpty;
  1211. // if not IsOK then
  1212. // begin
  1213. // IsOK := FileName.IndexOf(PathDelim) <> -1;
  1214. // end;
  1215. // end;
  1216. // end;
  1217. end;
  1218. if IsOK then
  1219. begin
  1220. FileName := FileName.Trim;
  1221. IsOK := not FileName.IsEmpty;
  1222. end;
  1223. // if IsOK then
  1224. // begin
  1225. //// IsOK := FileExists(FileName);
  1226. // IsOK := FileName.IndexOf(PathDelim) <> -1;
  1227. // end;
  1228. if Assigned(FGetFileNameCallBack1) then
  1229. FGetFileNameCallBack1(IsOK, FileName);
  1230. if Assigned(FGetFileNameCallBack2) then
  1231. FGetFileNameCallBack2(IsOK, FileName);
  1232. end;
  1233. function InternalOpenFileDialog(Title, FileExtension:string;
  1234. GetFileNameCallBack1: TGetFileNameListener;
  1235. GetFileNameCallBack2: TGetFileNameLIsternerMethod): Boolean;
  1236. var
  1237. MIMEType: JString;
  1238. mime: JMimeTypeMap;
  1239. Intent: JIntent;
  1240. IntentChooser: JIntent;
  1241. sMIMEType: string;
  1242. begin
  1243. Result := False;
  1244. FGetFileNameCallBack1 := GetFileNameCallBack1;
  1245. FGetFileNameCallBack2 := GetFileNameCallBack2;
  1246. if FileExtension.Substring(0,1) = '.' then
  1247. begin
  1248. FileExtension := FileExtension.Substring(1);
  1249. end;
  1250. if FileExtension = '*.*' then
  1251. begin
  1252. FileExtension := '*/*';
  1253. end;
  1254. MIMEType := nil;
  1255. sMIMEType := '';
  1256. if FileExtension = 'file/*' then
  1257. begin
  1258. sMIMEType := 'file/*';
  1259. end
  1260. else if FileExtension = '*' then
  1261. begin
  1262. if TOSVersion.Check(4, 4) then
  1263. begin
  1264. sMIMEType := '*/*';
  1265. end
  1266. else
  1267. begin
  1268. sMIMEType := 'file/*';
  1269. end;
  1270. end
  1271. else if FileExtension = '*/*' then
  1272. begin
  1273. sMIMEType := '*/*';
  1274. end
  1275. else if FileExtension = '' then
  1276. begin
  1277. sMIMEType := '*/*';
  1278. end
  1279. else
  1280. begin
  1281. FileExtension := AnsiLowerCase(FileExtension);
  1282. //http://www.oschina.net/code/snippet_1269559_25060
  1283. mime := TJMimeTypeMap.JavaClass.getSingleton;
  1284. if mime <> nil then
  1285. begin
  1286. MIMEType := mime.getMimeTypeFromExtension(StringToJString(FileExtension));
  1287. end;
  1288. end;
  1289. if MIMEType <> nil then
  1290. begin
  1291. sMIMEType := JStringToString(MIMEType).Trim;
  1292. end;
  1293. if sMIMEType.IsEmpty then
  1294. sMIMEType := '*/*'; // File/* 有时候打不开
  1295. //http://www.cnblogs.com/linlf03/archive/2013/08/19/3267732.html
  1296. // if TOSVersion.Check(4, 4) then
  1297. // begin
  1298. // Intent := TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_OPEN_DOCUMENT);
  1299. // end
  1300. // else
  1301. begin
  1302. Intent := TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_GET_CONTENT);
  1303. end;
  1304. Intent.setType(StringToJString(sMIMEType));
  1305. Intent.addCategory(TJIntent.JavaClass.CATEGORY_OPENABLE);
  1306. IntentChooser := TJIntent.JavaClass.createChooser(Intent, StrToJCharSequence(Title));
  1307. if FMessageChooserID <> 0 then
  1308. TMessageManager.DefaultManager.Unsubscribe(TMessageResultNotification, FMessageChooserID);
  1309. FMessageChooserID := 0;
  1310. FMessageChooserID := TMessageManager.DefaultManager.SubscribeToMessage(
  1311. TMessageResultNotification, HandleActivityMessageforChooser);
  1312. try
  1313. {$IF CompilerVersion >= 30.0} // >=RAD10
  1314. TAndroidHelper.Activity
  1315. {$ELSE}
  1316. SharedActivity
  1317. {$ENDIF}
  1318. .startActivityForResult(IntentChooser, FILE_SELECT_CODE);
  1319. Result := True;
  1320. except
  1321. raise Exception.Create(Error_NotFoundFileManager_Str);
  1322. end;
  1323. end;
  1324. function OpenFileDialog(Title, FileExtension:string; GetFileNameCallBack: TGetFileNameListener): Boolean;
  1325. begin
  1326. Result := InternalOpenFileDialog(Title, FileExtension, GetFileNameCallBack, nil);
  1327. end;
  1328. function OpenFileDialog(Title, FileExtension:string; GetFileNameCallBack: TGetFileNameLIsternerMethod): Boolean;
  1329. begin
  1330. Result := InternalOpenFileDialog(Title, FileExtension, nil, GetFileNameCallBack);
  1331. end;
  1332. var
  1333. FExterStoragePath: string = '';
  1334. FInnerStoragePath: string = '';
  1335. FVolumePaths: string = '';
  1336. FSDRegisteredReceiver: Boolean = False;
  1337. //type
  1338. // TSDBroadcastListener = class(TJavaLocal, JFMXBroadcastReceiverListener)
  1339. // private
  1340. // //[Weak] FTestObj: TForm1; //TForm1 是拿来测试,不是必须的。
  1341. // public
  1342. // constructor Create;
  1343. // //看上去,下面的函数属于线程中
  1344. // procedure onReceive(context: JContext; intent: JIntent); cdecl;
  1345. // end;
  1346. //
  1347. //var
  1348. // //安卓下消息3元素。
  1349. // FSDBroadcastReceiver: JFMXBroadcastReceiver;
  1350. // FSDIntentFilter: JIntentFilter;
  1351. // FSDBroadcastListener: TSDBroadcastListener;
  1352. // //你的 APP 安卓对象。
  1353. // FActivity: JNativeActivity;
  1354. //
  1355. //
  1356. //{ TSDBroadcastListener }
  1357. //
  1358. //constructor TSDBroadcastListener.Create;
  1359. //begin
  1360. // inherited Create;
  1361. // InnerStoragePath := GetSDCardPath(0);
  1362. // ExterStoragePath := GetSDCardPath(1);
  1363. // ExterStoragePathCanRead := False;
  1364. // ExterStoragePathCanWrite := False;
  1365. // SDMountedMessageReceived := False;
  1366. //end;
  1367. //
  1368. //procedure TSDBroadcastListener.onReceive(context: JContext; intent: JIntent);
  1369. //var
  1370. // Action,
  1371. // ExternalPath,
  1372. // TempStr1, TempStr2, TempStr3,
  1373. // TempStr: string;
  1374. // AJFile: JFile;
  1375. //begin
  1376. // //这里就是处理消息的地方。
  1377. // Action := JStringToString(intent.getAction);
  1378. // TempStr1 := JStringToString(TJIntent.JavaClass.ACTION_MEDIA_MOUNTED);
  1379. // TempStr2 := JStringToString(TJIntent.JavaClass.ACTION_MEDIA_UNMOUNTED);
  1380. // ExternalPath := IncludeTrailingPathDelimiter(GetExternalStoragePath);
  1381. // if SameText(Action, TempStr1) or SameText(Action, TempStr2) then
  1382. // begin
  1383. // TempStr := JStringToString(intent.getData.getPath);// 外置设备路径
  1384. // TempStr := IncludeTrailingPathDelimiter(TempStr);
  1385. // if TOSVersion.Check(5) and SameFileName(TempStr, ExternalPath) then
  1386. // begin
  1387. // //说明内外卡交换了。这是 5.0 的特点。
  1388. // InnerStoragePath := ExterStoragePath;
  1389. // ExterStoragePath := TempStr;
  1390. // end;
  1391. // if SameFileName(TempStr, ExterStoragePath) then
  1392. // begin
  1393. // if SameText(Action, TempStr1) then
  1394. // begin
  1395. // ExterStoragePathCanRead := True;
  1396. // AJFile := TJFile.JavaClass.init(
  1397. // StringToJString(ExcludeTrailingPathDelimiter(TempStr)));
  1398. // if AJFile <> nil then
  1399. // begin
  1400. // ExterStoragePathCanRead := AJFile.canRead;
  1401. // ExterStoragePathCanWrite := AJFile.canWrite;
  1402. // end;
  1403. // SDMountedMessageReceived := True;
  1404. // end;
  1405. // end;
  1406. // end;
  1407. // TempStr1 := JStringToString(TJIntent.JavaClass.ACTION_MEDIA_REMOVED);
  1408. // TempStr2 := JStringToString(TJIntent.JavaClass.ACTION_MEDIA_SHARED);
  1409. // TempStr3 := JStringToString(TJIntent.JavaClass.ACTION_MEDIA_EJECT);
  1410. // if SameText(Action, TempStr1) or
  1411. // SameText(Action, TempStr2) or SameText(Action, TempStr3) then
  1412. // begin
  1413. // TempStr := JStringToString(intent.getData.getPath);// 外置设备路径
  1414. // TempStr := IncludeTrailingPathDelimiter(TempStr);
  1415. // if SameFileName(TempStr, ExterStoragePath) then
  1416. // begin
  1417. // ExterStoragePathCanRead := False;
  1418. // ExterStoragePathCanWrite := False;
  1419. // end;
  1420. // end;
  1421. //end;
  1422. //
  1423. //procedure RegisterSDReceiver;
  1424. //begin
  1425. // if FSDRegisteredReceiver then exit;
  1426. // FActivity := TJNativeActivity.Wrap(PANativeActivity(System.DelphiActivity)^.clazz);
  1427. // //FActivity :=
  1428. //{$IF CompilerVersion >= 30.0} // >=RAD10
  1429. // TAndroidHelper.Activity
  1430. //{$ELSE}
  1431. // SharedActivity
  1432. //{$ENDIF};
  1433. // FSDBroadcastListener := TSDBroadcastListener.Create;
  1434. // FSDBroadcastReceiver := TJFMXBroadcastReceiver.JavaClass.init(FSDBroadcastListener);
  1435. //
  1436. // //http://blog.csdn.net/yigelangmandeshiren/article/details/8145059
  1437. // FSDIntentFilter := TJIntentFilter.JavaClass.init(TJIntent.JavaClass.ACTION_MEDIA_MOUNTED);
  1438. // FSDIntentFilter.setPriority(1000);// 设置最高优先级
  1439. // FSDIntentFilter.addAction(TJIntent.JavaClass.ACTION_MEDIA_EJECT);
  1440. // FSDIntentFilter.addAction(TJIntent.JavaClass.ACTION_MEDIA_REMOVED);
  1441. // FSDIntentFilter.addAction(TJIntent.JavaClass.ACTION_MEDIA_SHARED);
  1442. // FSDIntentFilter.addAction(TJIntent.JavaClass.ACTION_MEDIA_UNMOUNTED);
  1443. // FSDIntentFilter.addDataScheme(StringToJString('file'));
  1444. //
  1445. // //注册消息接收器。别忘了 unregisterReceiver(FBroadcastReceiver);
  1446. // FActivity.registerReceiver(FSDBroadcastReceiver, FSDIntentFilter);
  1447. //end;
  1448. //
  1449. //procedure UnRegisterSDReceiver;
  1450. //begin
  1451. // if not FSDRegisteredReceiver then exit;
  1452. // FActivity.unregisterReceiver(FSDBroadcastReceiver);
  1453. // FreeAndNil(FSDBroadcastListener);
  1454. // FSDBroadcastReceiver := nil;
  1455. // FSDIntentFilter := nil;
  1456. // FActivity := nil;
  1457. //end;
  1458. function GetIsExternalStorageRemovable: Boolean;
  1459. begin
  1460. Result := True;
  1461. try
  1462. Result := TJEnvironment.JavaClass.isExternalStorageRemovable;
  1463. except
  1464. //低版本可能发生错误。
  1465. end;
  1466. end;
  1467. function GetExterStoragePath: string;
  1468. //var
  1469. // AJstr: JString;
  1470. begin
  1471. if FExterStoragePath.Trim = '' then
  1472. begin
  1473. FExterStoragePath := GetSDCardPath(1);
  1474. if GetIsExternalStorageRemovable then
  1475. begin
  1476. FExterStoragePath := GetSDCardPath(0);
  1477. end;
  1478. end;
  1479. Result := FExterStoragePath.Trim;
  1480. if not DirectoryExists(ExcludeTrailingPathDelimiter(Result)) then
  1481. Result := '';
  1482. // AJstr := TJSystem.JavaClass.getenv(StringToJString('SECONDARY_STORAGE'));
  1483. // if AJstr <> nil then
  1484. // Result := IncludeTrailingPathDelimiter(JStringToString(AJstr));
  1485. end;
  1486. function GetInnerStoragePath: string;
  1487. //var
  1488. // AJstr: JString;
  1489. begin
  1490. if FInnerStoragePath.Trim = '' then
  1491. begin
  1492. FInnerStoragePath := GetSDCardPath(0);
  1493. if GetIsExternalStorageRemovable then
  1494. begin
  1495. FInnerStoragePath := GetSDCardPath(1);
  1496. end;
  1497. end;
  1498. Result := FInnerStoragePath.Trim;
  1499. if not DirectoryExists(ExcludeTrailingPathDelimiter(Result)) then
  1500. Result := '';
  1501. // ExterStoragePath := GetSDCardPath(1);
  1502. // AJstr := TJSystem.JavaClass.getenv(StringToJString('EXTERNAL_STORAGE'));
  1503. // if AJstr <> nil then
  1504. // Result := IncludeTrailingPathDelimiter(JStringToString(AJstr));
  1505. end;
  1506. function GetVolumePaths: string;
  1507. var
  1508. Sm: JStorageManager;
  1509. SmNative: JObject;
  1510. AList: TStrings;
  1511. VolumePaths: TJavaObjectArray<JString>;
  1512. I: Integer;
  1513. begin
  1514. Result := '';
  1515. if not IsCanFindJavaMethod('getVolumePaths',
  1516. '()[Ljava/lang/String;', 'android/os/storage/StorageManager') then
  1517. begin
  1518. //可以返回点别的。
  1519. exit;
  1520. end;
  1521. Sm := nil;
  1522. SmNative :=
  1523. {$IF CompilerVersion >= 30.0} // >=RAD10
  1524. TAndroidHelper.Context
  1525. {$ELSE}
  1526. SharedActivityContext
  1527. {$ENDIF}
  1528. .getSystemService(TJContext.JavaClass.STORAGE_SERVICE);
  1529. if SmNative <> nil then
  1530. begin
  1531. Sm := TJStorageManager.Wrap((SmNative as ILocalObject).GetObjectID);
  1532. end;
  1533. //不打算兼容 2.X 了。
  1534. if (Sm <> nil) and TOSVersion.Check(4) then
  1535. begin
  1536. AList := TStringList.Create;
  1537. try
  1538. try
  1539. VolumePaths := Sm.getVolumePaths;
  1540. for I := 0 to VolumePaths.Length - 1 do
  1541. begin
  1542. AList.Add(JStringToString(VolumePaths.Items[I]));
  1543. end;
  1544. except
  1545. end;
  1546. VolumePaths := nil;
  1547. Result := AList.Text;
  1548. FVolumePaths := Result;
  1549. finally
  1550. FreeAndNil(AList);
  1551. end;
  1552. end;
  1553. end;
  1554. //function GetActiveMemorySize: UInt64;
  1555. //var
  1556. // AStringVs,
  1557. // AStrings: TStringList;
  1558. // TempValue: string;
  1559. // ABufferedReader: JBufferedReader;
  1560. // V1, V2: UInt64;
  1561. //begin
  1562. // Result := 0;
  1563. // if FileExists('/proc/meminfo') then
  1564. // begin
  1565. // AStrings := TStringList.Create;
  1566. // AStringVs := TStringList.Create;
  1567. // try
  1568. // AStrings.LineBreak := sLineBreak;
  1569. // AStrings.NameValueSeparator := ':';
  1570. // AStrings.Clear;
  1571. // ABufferedReader := TJBufferedReader.JavaClass.init(
  1572. // JReader(TJFileReader.JavaClass.init(StringToJString('/proc/meminfo'))));
  1573. // repeat
  1574. // TempValue := JStringToString(ABufferedReader.readLine);
  1575. // if TempValue <> '' then
  1576. // begin
  1577. // AStrings.Add(TempValue);
  1578. // end;
  1579. // until (not ABufferedReader.ready);
  1580. // ABufferedReader.close;
  1581. // V1 := 0;
  1582. // TempValue := AStrings.Values['Mapped'].Trim;
  1583. // AStringVs.Clear;
  1584. // AStringVs.NameValueSeparator := ' ';
  1585. // AStringVs.Add(TempValue.Trim);
  1586. // TempValue := AStringVs.Names[0];
  1587. // V2 := StrToInt64Def(TempValue, 0);
  1588. // V1 := V1 + V2;
  1589. // TempValue := AStrings.Values['Inactive'].Trim;
  1590. // AStringVs.Clear;
  1591. // AStringVs.NameValueSeparator := ' ';
  1592. // AStringVs.Add(TempValue.Trim);
  1593. // TempValue := AStringVs.Names[0];
  1594. // V2 := StrToInt64Def(TempValue, 0);
  1595. // V1 := V1 + V2;
  1596. // Result := V1 * 1024;
  1597. // finally
  1598. // FreeAndNil(AStringVs);
  1599. // FreeAndNil(AStrings);
  1600. // end;
  1601. // end;
  1602. // if Result = 0 then
  1603. // begin
  1604. // Result := GetFreeMemorySize;
  1605. // end;
  1606. //end;
  1607. function GetScreenClientInches: Single;
  1608. var
  1609. x,y: Double;
  1610. dm: JDisplayMetrics;
  1611. begin
  1612. Result := 3;
  1613. dm := GetJDisplayMetrics;
  1614. if dm = nil then exit;
  1615. x := dm.widthPixels;
  1616. y := dm.heightPixels;
  1617. // x := System.Math.Power(x / dm.xdpi, 2);
  1618. // y := System.Math.Power(y / dm.ydpi, 2);
  1619. // Result := Sqrt(x + y);
  1620. if (dm.densityDpi > dm.xdpi) and (dm.densityDpi > dm.ydpi) then
  1621. begin
  1622. Result := Sqrt(x * x + y * y ) / dm.densityDpi;
  1623. end
  1624. else
  1625. begin
  1626. Result := Sqrt((x * x / dm.xdpi / dm.xdpi) + (y * y / dm.ydpi / dm.ydpi));
  1627. end;
  1628. end;
  1629. function IsCanFindJavaClass(const NamePath: string): Boolean;
  1630. var
  1631. PEnv: PJNIEnv;
  1632. {$IF CompilerVersion < 30.0} // < RAD10
  1633. PActivity: PANativeActivity;
  1634. {$ELSE}
  1635. ContextClass: JNIClass;
  1636. {$ENDIF}
  1637. TempClass: JNIClass;
  1638. AMS: MarshaledAString;
  1639. begin
  1640. try
  1641. TempClass := nil;
  1642. {$IF CompilerVersion < 30.0} // < RAD10
  1643. PActivity := PANativeActivity(System.DelphiActivity);
  1644. PActivity^.vm^.AttachCurrentThread(PActivity^.vm, @PEnv, nil);
  1645. {$ELSE}
  1646. PJavaVM(System.JavaMachine)^.AttachCurrentThread(System.JavaMachine, @PEnv, nil);
  1647. ContextClass := nil;
  1648. ContextClass := PEnv^.GetObjectClass(PEnv, System.JavaContext);
  1649. {$ENDIF}
  1650. AMS := MarshaledAString(Utf8Encode(NamePath.Trim.Replace('.', '/', [rfReplaceAll])));
  1651. try
  1652. TempClass := PEnv^.FindClass(PEnv, AMS);
  1653. PEnv^.ExceptionClear(PEnv);
  1654. Result := TempClass <> nil;
  1655. finally
  1656. if TempClass <> nil then
  1657. PEnv^.DeleteLocalRef(PEnv, TempClass);
  1658. {$IF CompilerVersion < 30.0} // < RAD10
  1659. {$ELSE}
  1660. if ContextClass <> nil then
  1661. PEnv^.DeleteLocalRef(PEnv, ContextClass);
  1662. {$ENDIF}
  1663. end;
  1664. except
  1665. Result := False;
  1666. end;
  1667. end;
  1668. function IsCanFindJavaMethod(const MethodName, Signature: string; const CalssNamePath: string = ''): Boolean;
  1669. var
  1670. PEnv: PJNIEnv;
  1671. {$IF CompilerVersion < 30.0} // < RAD10
  1672. PActivity: PANativeActivity;
  1673. {$ELSE}
  1674. ContextClass: JNIClass;
  1675. {$ENDIF}
  1676. ActivityClass: JNIClass;
  1677. GetMethod: JNIMethodID;
  1678. ASignature: string;
  1679. AMSMethodName, AMSSignature: MarshaledAString;
  1680. begin
  1681. try
  1682. AMSMethodName := MarshaledAString(Utf8Encode(MethodName.Trim));
  1683. {$IF CompilerVersion < 30.0} // < RAD10
  1684. PActivity := PANativeActivity(System.DelphiActivity);
  1685. PActivity^.vm^.AttachCurrentThread(PActivity^.vm, @PEnv, nil);
  1686. {$ELSE}
  1687. PJavaVM(System.JavaMachine)^.AttachCurrentThread(System.JavaMachine, @PEnv, nil);
  1688. ContextClass := nil;
  1689. ContextClass := PEnv^.GetObjectClass(PEnv, System.JavaContext);
  1690. {$ENDIF}
  1691. ActivityClass := nil;
  1692. try
  1693. if CalssNamePath.Trim <> '' then
  1694. begin
  1695. ASignature := CalssNamePath.Trim.Replace('.', '/', [rfReplaceAll]);
  1696. AMSSignature := MarshaledAString(Utf8Encode(ASignature));
  1697. ActivityClass := PEnv^.FindClass(PEnv, AMSSignature);
  1698. PEnv^.ExceptionClear(PEnv);
  1699. end
  1700. else
  1701. begin
  1702. {$IF CompilerVersion < 30.0} // < RAD10
  1703. ActivityClass := PEnv^.GetObjectClass(PEnv, PActivity^.clazz);
  1704. {$ELSE}
  1705. ActivityClass := PEnv^.GetObjectClass(PEnv, System.JavaContext);
  1706. {$ENDIF}
  1707. end;
  1708. if ActivityClass <> nil then
  1709. begin
  1710. ASignature := Signature.Trim;
  1711. AMSSignature := MarshaledAString(Utf8Encode(ASignature));
  1712. GetMethod := PEnv^.GetMethodID(PEnv, ActivityClass, AMSMethodName, AMSSignature);
  1713. PEnv^.ExceptionClear(PEnv);
  1714. Result := GetMethod <> nil;
  1715. end;
  1716. finally
  1717. if ActivityClass <> nil then
  1718. PEnv^.DeleteLocalRef(PEnv, ActivityClass);
  1719. {$IF CompilerVersion < 30.0} // < RAD10
  1720. {$ELSE}
  1721. if ContextClass <> nil then
  1722. PEnv^.DeleteLocalRef(PEnv, ContextClass);
  1723. {$ENDIF}
  1724. end;
  1725. except
  1726. Result := False;
  1727. end;
  1728. end;
  1729. function IsCanFindJavaStaticMethod(const MethodName, Signature: string; const CalssNamePath: string = ''): Boolean;
  1730. var
  1731. PEnv: PJNIEnv;
  1732. {$IF CompilerVersion < 30.0} // < RAD10
  1733. PActivity: PANativeActivity;
  1734. {$ELSE}
  1735. ContextClass: JNIClass;
  1736. {$ENDIF}
  1737. ActivityClass: JNIClass;
  1738. GetMethod: JNIMethodID;
  1739. ASignature: string;
  1740. AMSMethodName, AMSSignature: MarshaledAString;
  1741. begin
  1742. try
  1743. AMSMethodName := MarshaledAString(Utf8Encode(MethodName.Trim));
  1744. {$IF CompilerVersion < 30.0} // < RAD10
  1745. PActivity := PANativeActivity(System.DelphiActivity);
  1746. PActivity^.vm^.AttachCurrentThread(PActivity^.vm, @PEnv, nil);
  1747. {$ELSE}
  1748. PJavaVM(System.JavaMachine)^.AttachCurrentThread(System.JavaMachine, @PEnv, nil);
  1749. ContextClass := nil;
  1750. ContextClass := PEnv^.GetObjectClass(PEnv, System.JavaContext);
  1751. {$ENDIF}
  1752. ActivityClass := nil;
  1753. try
  1754. if CalssNamePath.Trim <> '' then
  1755. begin
  1756. ASignature := CalssNamePath.Trim.Replace('.', '/', [rfReplaceAll]);
  1757. AMSSignature := MarshaledAString(Utf8Encode(ASignature));
  1758. ActivityClass := PEnv^.FindClass(PEnv, AMSSignature);
  1759. PEnv^.ExceptionClear(PEnv);
  1760. end
  1761. else
  1762. begin
  1763. {$IF CompilerVersion < 30.0} // < RAD10
  1764. ActivityClass := PEnv^.GetObjectClass(PEnv, PActivity^.clazz);
  1765. {$ELSE}
  1766. ActivityClass := PEnv^.GetObjectClass(PEnv, System.JavaContext);
  1767. {$ENDIF}
  1768. end;
  1769. if ActivityClass <> nil then
  1770. begin
  1771. ASignature := Signature.Trim;
  1772. AMSSignature := MarshaledAString(Utf8Encode(ASignature));
  1773. GetMethod := PEnv^.GetStaticMethodID(PEnv, ActivityClass, AMSMethodName, AMSSignature);
  1774. PEnv^.ExceptionClear(PEnv);
  1775. Result := GetMethod <> nil;
  1776. end;
  1777. finally
  1778. if ActivityClass <> nil then
  1779. PEnv^.DeleteLocalRef(PEnv, ActivityClass);
  1780. {$IF CompilerVersion < 30.0} // < RAD10
  1781. {$ELSE}
  1782. if ContextClass <> nil then
  1783. PEnv^.DeleteLocalRef(PEnv, ContextClass);
  1784. {$ENDIF}
  1785. end;
  1786. except
  1787. Result := False;
  1788. end;
  1789. end;
  1790. procedure UpdateAlbum(FileNames: string);
  1791. var
  1792. AJStrList: TJavaObjectArray<JString>;
  1793. I: Integer;
  1794. begin
  1795. With TStringList.Create do
  1796. begin
  1797. try
  1798. Text := FileNames;
  1799. AJStrList := TJavaObjectArray<JString>.Create(Count);
  1800. for I := 0 to Count - 1 do
  1801. begin
  1802. AJStrList.Items[I] := StringToJString(Strings[I]);
  1803. end;
  1804. finally
  1805. Free;
  1806. end;
  1807. end;
  1808. TJMediaScannerConnection.JavaClass.scanFile(TAndroidHelper.Context, AJStrList,
  1809. nil, nil);
  1810. end;
  1811. function ReadFileToString(const AFileName: string): string;
  1812. var
  1813. AReader: JReader;
  1814. ABufferedReader: JBufferedReader;
  1815. TempValue: string;
  1816. begin
  1817. Result := '';
  1818. if not FileExists(AFileName) then exit;
  1819. AReader := TJFileReader.JavaClass.init(StringToJString(AFileName)) as JReader;
  1820. ABufferedReader := TJBufferedReader.JavaClass.init(AReader);
  1821. repeat
  1822. TempValue := JStringToString(ABufferedReader.readLine);
  1823. if TempValue <> '' then
  1824. begin
  1825. Result := Result + sLineBreak + TempValue;
  1826. end;
  1827. until (not ABufferedReader.ready);
  1828. ABufferedReader.close;
  1829. end;
  1830. function ReadNoSizeFileToString(const AFileName: string): string;
  1831. begin
  1832. Result := '';
  1833. if not FileExists(AFileName) then exit;
  1834. With TFileStream.Create(AFileName, fmOpenRead) do
  1835. try
  1836. if Size = -1 then exit;
  1837. finally
  1838. Free;
  1839. end;
  1840. Result := ReadFileToString(AFileName);
  1841. end;
  1842. {$ENDIF}
  1843. {$IFDEF ANDROID}
  1844. function GetJniPath(MethodName, Signature: MarshaledAString): string;
  1845. var
  1846. PEnv: PJniEnv;
  1847. ActivityClass: JNIClass;
  1848. FileClass: JNIClass;
  1849. GetMethod: JNIMethodID;
  1850. GetPathMethod: JNIMethodID;
  1851. PActivity: PANativeActivity;
  1852. StrPathObject: JNIObject;
  1853. FileObject: JNIObject;
  1854. begin
  1855. PActivity := PANativeActivity(System.DelphiActivity);
  1856. PActivity^.vm^.AttachCurrentThread(PActivity^.vm, @PEnv, nil);
  1857. ActivityClass := PEnv^.GetObjectClass(PEnv, PActivity^.clazz);
  1858. GetMethod := PEnv^.GetMethodID(PEnv, ActivityClass, MethodName, Signature);
  1859. FileObject := PEnv^.CallObjectMethodA(PEnv, PActivity^.clazz, GetMethod, PJNIValue(ArgsToJNIValues([nil])));
  1860. if FileObject = nil then
  1861. Exit('');
  1862. FileClass := PEnv^.GetObjectClass(PEnv, FileObject);
  1863. GetPathMethod := PEnv^.GetMethodID(PEnv, FileClass, 'getPath', '()Ljava/lang/String;');
  1864. StrPathObject := PEnv^.CallObjectMethodA(PEnv, FileObject, GetPathMethod, PJNIValue(ArgsToJNIValues([])));
  1865. Result := JNIStringToString(PEnv, StrPathObject);
  1866. PEnv^.DeleteLocalRef(PEnv, StrPathObject);
  1867. PEnv^.DeleteLocalRef(PEnv, FileClass);
  1868. PEnv^.DeleteLocalRef(PEnv, FileObject);
  1869. PEnv^.DeleteLocalRef(PEnv, ActivityClass);
  1870. end;
  1871. function JNIgetExternalStorageDirectory(SubPath: string): string;
  1872. begin
  1873. // if SubPath <> '' then
  1874. // begin
  1875. // Result := IncludeTrailingPathDelimiter(
  1876. // GetJniPath('getExternalStorageDirectory', '()Landroid/os/Environment;')
  1877. // ) + SubPath;
  1878. // end
  1879. // else
  1880. // begin
  1881. // Result := GetJniPath('getExternalStorageDirectory', '()Landroid/os/Environment;');
  1882. // end;
  1883. try
  1884. if SubPath <> '' then
  1885. begin
  1886. Result := IncludeTrailingPathDelimiter(JStringToString(
  1887. TJEnvironment.JavaClass.getExternalStorageDirectory.getPath)) + SubPath;
  1888. end
  1889. else
  1890. begin
  1891. Result := JStringToString(TJEnvironment.JavaClass.getExternalStorageDirectory.getPath);
  1892. end;
  1893. except
  1894. //低版本可能发生错误。
  1895. end;
  1896. end;
  1897. function GetExternalStoragePath: string;
  1898. begin
  1899. Result := IncludeTrailingPathDelimiter(JNIgetExternalStorageDirectory(''));
  1900. end;
  1901. //function isPathCanWrite(const PathOrDir: string; const Default: Boolean = True): Boolean;
  1902. //var
  1903. // ADir: string;
  1904. //begin
  1905. // Result := False;
  1906. // ADir := ExcludeTrailingPathDelimiter(PathOrDir);
  1907. // if not DirectoryExists(ADir) then exit;
  1908. // Result := True;
  1909. // try
  1910. ////明明无权限写入,却返回 canWrite 为 True
  1911. // if not TJFile.JavaClass.init(StringToJString(ADir)).canWrite then
  1912. // begin
  1913. // Result := False;
  1914. // end;
  1915. // except
  1916. // Result := False;
  1917. // end;
  1918. //end;
  1919. function isPathCanUseNow(const PathOrDir: string; const Default: Boolean = True): Boolean;
  1920. var
  1921. ADir: string;
  1922. begin
  1923. Result := False;
  1924. ADir := ExcludeTrailingPathDelimiter(PathOrDir);
  1925. if not DirectoryExists(ADir) then exit;
  1926. try
  1927. // //下面的代码不能正确的区分内外 SD。
  1928. // Result := TJEnvironment.JavaClass.getExternalStorageState.equals(TJEnvironment.JavaClass.MEDIA_MOUNTED);
  1929. // if GetExterStoragePath.Trim = '' then exit;
  1930. Result := True;
  1931. // if FindJavaMethod('getStorageState',
  1932. // '(Ljava/io/File;)Ljava/lang/String;', 'android/os/Environment') then
  1933. // begin
  1934. // // 这个接口 5.0 会死锁。还是不用了。
  1935. // if not TJEnvironment.JavaClass.getStorageState(
  1936. // TJFile.JavaClass.init(StringToJString(ADir))).equals(
  1937. // TJEnvironment.JavaClass.MEDIA_MOUNTED) then
  1938. // begin
  1939. // Result := False;
  1940. // end;
  1941. // end
  1942. // else
  1943. begin
  1944. if not TJFile.JavaClass.init(StringToJString(ADir)).canRead then
  1945. begin
  1946. Result := False;
  1947. end;
  1948. end;
  1949. except
  1950. end;
  1951. end;
  1952. {$ELSE}
  1953. function isPathCanUseNow(const PathOrDir: string; const Default: Boolean = True): Boolean;
  1954. begin
  1955. Result := False;
  1956. if not DirectoryExists(ExcludeTrailingPathDelimiter(PathOrDir)) then exit;
  1957. Result := Default;
  1958. end;
  1959. {$ENDIF ANDROID}
  1960. function TestPathCanWrite(const PathOrDir: string): Boolean;
  1961. var
  1962. ATempFile,
  1963. ADir: string;
  1964. AHandle: THandle;
  1965. begin
  1966. Result := False;
  1967. ADir := ExcludeTrailingPathDelimiter(PathOrDir);
  1968. if not DirectoryExists(ADir) then exit;
  1969. repeat
  1970. ATempFile := System.IOUtils.TPath.GetTempFileName;
  1971. try
  1972. if FileExists(ATempFile) then
  1973. System.IOUtils.TFile.Delete(ATempFile);
  1974. except
  1975. end;
  1976. ATempFile := IncludeTrailingPathDelimiter(ADir) + ExtractFileName(ATempFile);
  1977. until not FileExists(ATempFile);
  1978. // ATempFile := GetTempFileName(ADir);
  1979. // if FileExists(ATempFile) then
  1980. // begin
  1981. // System.IOUtils.TFile.Delete(ATempFile);
  1982. // end;
  1983. try
  1984. AHandle := INVALID_HANDLE_VALUE;
  1985. {$IF Defined(MSWINDOWS)}
  1986. AHandle := FileCreate(ATempFile, 0);
  1987. {$ELSEIF Defined(POSIX)}
  1988. AHandle := FileCreate(ATempFile, FileAccessRights);
  1989. {$ENDIF POSIX}
  1990. if AHandle = INVALID_HANDLE_VALUE then
  1991. begin
  1992. Result := False;
  1993. end
  1994. else
  1995. begin
  1996. Result := True;
  1997. FileClose(AHandle);
  1998. end;
  1999. except
  2000. Result := False;
  2001. end;
  2002. if FileExists(ATempFile) then
  2003. begin
  2004. System.IOUtils.TFile.Delete(ATempFile);
  2005. Result := True;
  2006. end;
  2007. end;
  2008. //function GetTempFileName(const ATempPath: string): string;
  2009. //{$IFDEF MSWINDOWS}
  2010. //var
  2011. // ErrCode: UINT;
  2012. //begin
  2013. // SetLength(Result, MAX_PATH);
  2014. //
  2015. // SetLastError(ERROR_SUCCESS);
  2016. // ErrCode := Winapi.Windows.GetTempFileName(PChar(
  2017. // IncludeTrailingPathDelimiter(ATempPath)
  2018. // ), 'tmp', 0, PChar(Result)); // DO NOT LOCALIZE
  2019. // if ErrCode = 0 then
  2020. // raise EInOutError.Create(SysErrorMessage(GetLastError));
  2021. //
  2022. // SetLength(Result, StrLen(PChar(Result)));
  2023. // if FileExists(Result) then
  2024. // begin
  2025. // System.IOUtils.TFile.Delete(Result);
  2026. // end;
  2027. //end;
  2028. //{$ENDIF}
  2029. //{$IFDEF POSIX}
  2030. //var
  2031. // LTempPath: TBytes;
  2032. // M: TMarshaller;
  2033. // LRet: MarshaledAString;
  2034. //begin
  2035. //// char * tempnam(const char *dir, const char *pfx);
  2036. //
  2037. // { Obtain a temporary file name }
  2038. // // This code changed from getting the temp name from the temp path via system
  2039. // // to get the temp name inside the specified temp path. We get the system temp path.
  2040. //// LTempPath := TEncoding.UTF8.GetBytes(string(tmpnam(nil)));
  2041. // LRet := tempnam(MarshaledAString(M.AsUTF8(
  2042. // //返回的路径没有包含 ATempPath
  2043. // IncludeTrailingPathDelimiter(ATempPath)
  2044. // ).ToPointer),nil);
  2045. // LTempPath := TEncoding.UTF8.GetBytes(string(LRet));
  2046. // free(LRet);
  2047. //
  2048. // { Convert to UTF16 or leave blank on possible error }
  2049. // if LTempPath <> nil then
  2050. // Result := TEncoding.UTF8.GetString(LTempPath)
  2051. // else
  2052. // Result := '';
  2053. //end;
  2054. //{$ENDIF POSIX}
  2055. {$IFDEF ANDROID}
  2056. function FileSystemAttributes(const Path: string): TFileSystemAttributes;
  2057. begin
  2058. Result := [fsSymLink, fsCaseSensitive];
  2059. // For android platform we can use the function PathConf on the same way
  2060. // that is used on IOS, but the problem is that for android we only can check
  2061. // _PC_2_SYMLINKS name, and that call is failing on version 2.3.3.
  2062. end;
  2063. {$ENDIF ANDROID}
  2064. function ExpandFileNameCase2(const FileName, RootPath: string; out MatchFound: TFilenameCaseMatch): string;
  2065. var
  2066. SR: System.SysUtils.TSearchRec;
  2067. FullPath, Name: string;
  2068. Status: Integer;
  2069. {$IFDEF POSIX}
  2070. FoundOne: Boolean;
  2071. Scans: Byte;
  2072. FirstLetter, TestLetter: string;
  2073. {$ENDIF POSIX}
  2074. begin
  2075. Result := ExpandFileName(FileName);
  2076. MatchFound := mkNone;
  2077. if FileName = '' then // Stop for empty strings, otherwise we risk to get info infinite loop.
  2078. Exit;
  2079. FullPath := ExtractFilePath(Result);
  2080. Name := ExtractFileName(Result);
  2081. // if FullPath is not the root directory (portable)
  2082. if not SameFileName(FullPath, IncludeTrailingPathDelimiter(ExtractFileDrive(FullPath))) then
  2083. begin // Does the path need case-sensitive work?
  2084. Status := FindFirst(ExcludeTrailingPathDelimiter(FullPath), faAnyFile, SR);
  2085. System.SysUtils.FindClose(SR); // close search before going recursive
  2086. if Status <> 0 then
  2087. begin
  2088. if SameFileName(IncludeTrailingPathDelimiter(FullPath), IncludeTrailingPathDelimiter(RootPath)) then
  2089. begin
  2090. end
  2091. else
  2092. begin
  2093. FullPath := ExcludeTrailingPathDelimiter(FullPath);
  2094. FullPath := ExpandFileNameCase2(FullPath, RootPath, MatchFound);
  2095. if MatchFound = mkNone then
  2096. Exit; // if we can't find the path, we certainly can't find the file!
  2097. end;
  2098. FullPath := IncludeTrailingPathDelimiter(FullPath);
  2099. end;
  2100. end;
  2101. // Path is validated / adjusted. Now for the file itself
  2102. try
  2103. if System.SysUtils.FindFirst(FullPath + Name, faAnyFile, SR)= 0 then // exact match on filename
  2104. begin
  2105. if not (MatchFound in [mkSingleMatch, mkAmbiguous]) then // path might have been inexact
  2106. begin
  2107. if Name = SR.Name then
  2108. MatchFound := mkExactMatch
  2109. else
  2110. MatchFound := mkSingleMatch;
  2111. end;
  2112. Exit(FullPath + SR.Name);
  2113. end;
  2114. finally
  2115. System.SysUtils.FindClose(SR);
  2116. end;
  2117. {$IFDEF POSIX}
  2118. { Scan the directory.
  2119. To minimize the number of filenames tested, scan the directory
  2120. using upper/lowercase first letter + wildcard.
  2121. This results in two scans of the directory (particularly on Linux) but
  2122. vastly reduces the number of times we have to perform an expensive
  2123. locale-charset case-insensitive string compare. }
  2124. FoundOne := False;
  2125. if (fsCaseSensitive in FileSystemAttributes(FullPath)) or
  2126. (fsCasePreserving in FileSystemAttributes(FullPath)) then
  2127. begin
  2128. // First, scan for lowercase first letter
  2129. FirstLetter := AnsiLowerCase(Name[Low(string)]);
  2130. for Scans := 0 to 1 do
  2131. begin
  2132. Status := FindFirst(FullPath + FirstLetter + '*', faAnyFile, SR);
  2133. while Status = 0 do
  2134. begin
  2135. if AnsiSameText(SR.Name, Name) then
  2136. begin
  2137. if FoundOne then
  2138. begin // this is the second match
  2139. MatchFound := mkAmbiguous;
  2140. Exit;
  2141. end
  2142. else
  2143. begin
  2144. FoundOne := True;
  2145. Result := FullPath + SR.Name;
  2146. end;
  2147. end;
  2148. Status := FindNext(SR);
  2149. end;
  2150. FindClose(SR);
  2151. TestLetter := AnsiUpperCase(Name[Low(string)]);
  2152. if TestLetter = FirstLetter then
  2153. Break;
  2154. FirstLetter := TestLetter;
  2155. end;
  2156. if MatchFound <> mkAmbiguous then
  2157. begin
  2158. if FoundOne then
  2159. MatchFound := mkSingleMatch
  2160. else
  2161. MatchFound := mkNone;
  2162. end;
  2163. end;
  2164. {$ENDIF POSIX}
  2165. end;
  2166. function BuildFileListInAPath(const Path: string; const Attr: Integer;
  2167. JustFile: Boolean = False): string;
  2168. var
  2169. AList: TStringList;
  2170. begin
  2171. Result := '';
  2172. AList := TStringList.Create;
  2173. try
  2174. BuildFileListInAPath(Path, Attr, AList, JustFile);
  2175. Result := AList.Text;
  2176. finally
  2177. FreeAndNil(AList);
  2178. end;
  2179. end;
  2180. function BuildFileListInAPath(const Path: string; const Attr: Integer; const List: TStrings;
  2181. JustFile: Boolean = False): Boolean;
  2182. var
  2183. SearchRec: TSearchRec;
  2184. R: Integer;
  2185. begin
  2186. Assert(List <> nil);
  2187. R := System.SysUtils.FindFirst(ExcludeTrailingPathDelimiter(Path), Attr, SearchRec);
  2188. Result := R = 0;
  2189. try
  2190. if Result then
  2191. begin
  2192. while R = 0 do
  2193. begin
  2194. if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
  2195. begin
  2196. if ((SearchRec.Attr and faDirectory) = faDirectory) then
  2197. begin
  2198. if not JustFile then
  2199. List.Add(SearchRec.Name);
  2200. end
  2201. else
  2202. List.Add(SearchRec.Name);
  2203. end;
  2204. R := System.SysUtils.FindNext(SearchRec);
  2205. end;
  2206. Result := R = 0;
  2207. end;
  2208. finally
  2209. System.SysUtils.FindClose(SearchRec);
  2210. end;
  2211. end;
  2212. function faAttrToFileAttributes(
  2213. const Attributes: Integer): TFileAttributes;
  2214. begin
  2215. {$IFDEF MSWINDOWS}
  2216. Result := [];
  2217. if Attributes and faReadOnly <> 0 then
  2218. Include(Result, TFileAttribute.faReadOnly);
  2219. if Attributes and faHidden <> 0 then
  2220. Include(Result, TFileAttribute.faHidden);
  2221. if Attributes and faSysFile <> 0 then
  2222. Include(Result, TFileAttribute.faSystem);
  2223. if Attributes and faDirectory <> 0 then
  2224. Include(Result, TFileAttribute.faDirectory);
  2225. if Attributes and faArchive <> 0 then
  2226. Include(Result, TFileAttribute.faArchive);
  2227. if Attributes and faSymLink <> 0 then
  2228. Include(Result, TFileAttribute.faSymLink);
  2229. if Attributes and faNormal <> 0 then
  2230. Include(Result, TFileAttribute.faNormal);
  2231. if Attributes and faTemporary <> 0 then
  2232. Include(Result, TFileAttribute.faTemporary);
  2233. if Attributes and faCompressed <> 0 then
  2234. Include(Result, TFileAttribute.faCompressed);
  2235. if Attributes and faEncrypted <> 0 then
  2236. Include(Result, TFileAttribute.faEncrypted);
  2237. {$ENDIF MSWINDOWS}
  2238. {$IFDEF POSIX}
  2239. Result := [];
  2240. if Attributes and faDirectory <> 0 then
  2241. Include(Result, TFileAttribute.faDirectory);
  2242. if Attributes and faArchive <> 0 then
  2243. Include(Result, TFileAttribute.faNormal);
  2244. if Attributes and faSymLink <> 0 then
  2245. Include(Result, TFileAttribute.faSymLink);
  2246. if Attributes and faNormal <> 0 then
  2247. Include(Result, TFileAttribute.faNormal);
  2248. {$ENDIF POSIX}
  2249. end;
  2250. {$IFDEF MSWINDOWS}
  2251. {$ELSE}
  2252. //好像不一定有效果。
  2253. function FileSetAttr(const FileName: string; Attr: Integer; FollowLink: Boolean = True): Integer;
  2254. var
  2255. AFileAttributes: TFileAttributes;
  2256. begin
  2257. // Result := faInvalid;
  2258. // AFileAttributes := faAttrToFileAttributes(Attr);
  2259. // TFile.SetAttributes(FileName, AFileAttributes);
  2260. Result := 0;
  2261. end;
  2262. {$ENDIF MSWINDOWS}
  2263. function DeleteTreeByEcho(const Source: string; AbortOnFailure: Boolean = False;
  2264. YesToAll: Boolean = True;
  2265. WaitMinSecond: Integer = DeleteDirectories_WaitMinSecond): Boolean;
  2266. var
  2267. Files: TStringList;
  2268. LPath: string; // writable copy of Path
  2269. FileName: string;
  2270. I: Integer;
  2271. PartialResult: Boolean;
  2272. Attr: Integer;
  2273. isDir: Boolean;
  2274. ToDeleteDir: string;
  2275. T: TDateTime;
  2276. FWaitSecond, FWaitMinSecond: Integer;
  2277. begin
  2278. Result := False;
  2279. if not DirectoryExists(ExtractFileDir(Source)) then
  2280. begin
  2281. exit;
  2282. end;
  2283. if Trim(Source) = PathDelim then
  2284. begin
  2285. exit;
  2286. end;
  2287. isDir := DirectoryExists(Source);
  2288. Result := True;
  2289. Files := TStringList.Create;
  2290. try
  2291. if isDir then
  2292. begin
  2293. LPath := IncludeTrailingPathDelimiter(Source);
  2294. ToDeleteDir := LPath + '\*.*';
  2295. end
  2296. else
  2297. begin
  2298. LPath := IncludeTrailingPathDelimiter(ExtractFilePath(Source));
  2299. ToDeleteDir := Source;
  2300. end;
  2301. BuildFileListInAPath(ToDeleteDir, faAnyFile, Files);
  2302. for I := 0 to Files.Count - 1 do
  2303. begin
  2304. FileName := LPath + PathDelim + Files[I];
  2305. PartialResult := True;
  2306. // If the current file is itself a directory then recursively delete it
  2307. Attr := FileGetAttr(FileName);
  2308. if (Attr <> faInvalid) and ((Attr and faDirectory) <> 0) then
  2309. begin
  2310. PartialResult := DeleteTreeByEcho(FileName, AbortOnFailure, YesToAll,
  2311. WaitMinSecond);
  2312. end
  2313. else
  2314. begin
  2315. if YesToAll then
  2316. begin
  2317. // Set attributes to normal in case it's a readonly file
  2318. PartialResult := FileSetAttr(FileName, faNormal) = 0;
  2319. end
  2320. else
  2321. begin
  2322. if ((Attr and faSysFile) <> 0) or ((Attr and faReadOnly) <> 0) or (Attr = faInvalid)
  2323. then
  2324. begin
  2325. PartialResult := False;
  2326. end;
  2327. end;
  2328. if PartialResult then
  2329. PartialResult := System.SysUtils.DeleteFile(FileName); //TFile.Delete()
  2330. end;
  2331. if not PartialResult then
  2332. begin
  2333. Result := False;
  2334. if AbortOnFailure then
  2335. begin
  2336. Break;
  2337. end;
  2338. end;
  2339. end;
  2340. finally
  2341. FreeAndNil(Files);
  2342. end;
  2343. if Result and isDir then
  2344. begin
  2345. if YesToAll then
  2346. begin
  2347. // Finally remove the directory itself
  2348. Result := FileSetAttr(LPath, faNormal or faDirectory) = 0;
  2349. end
  2350. else
  2351. begin
  2352. Attr := FileGetAttr(LPath);
  2353. if ((Attr and faSysFile) <> 0) or ((Attr and faReadOnly) <> 0) or (Attr = faInvalid) then
  2354. begin
  2355. Result := False;
  2356. end;
  2357. end;
  2358. if Result then
  2359. begin
  2360. {$IOCHECKS OFF}
  2361. RmDir(LPath);
  2362. T := Now;
  2363. FWaitSecond := WaitMinSecond div 1000;
  2364. FWaitMinSecond := WaitMinSecond mod 1000;
  2365. while DirectoryExists(LPath) do
  2366. begin
  2367. if T + EncodeTime(0, 0, FWaitSecond, FWaitMinSecond) < now then
  2368. begin
  2369. break;
  2370. end;
  2371. Sleep(1);
  2372. end;
  2373. {$IFDEF IOCHECKS_ON}
  2374. {$IOCHECKS ON}
  2375. {$ENDIF IOCHECKS_ON}
  2376. // Result := IOResult = 0;
  2377. Result := not DirectoryExists(LPath);
  2378. // if not Result then
  2379. // begin
  2380. // ShowMessage(LPath);
  2381. // end;
  2382. end;
  2383. end;
  2384. end;
  2385. function DeleteDirectoryByEcho(const Source: string;
  2386. AbortOnFailure: Boolean = False; YesToAll: Boolean = True;
  2387. WaitMinSecond: Integer = DeleteDirectories_WaitMinSecond): Boolean;
  2388. var
  2389. T: TDateTime;
  2390. FWaitSecond, FWaitMinSecond: Integer;
  2391. begin
  2392. Result := False;
  2393. if not DirectoryExists(ExtractFileDir(Source)) then
  2394. begin
  2395. exit;
  2396. end;
  2397. if Trim(Source) = PathDelim then
  2398. begin
  2399. exit;
  2400. end;
  2401. Result := DeleteTreeByEcho(Source, AbortOnFailure, YesToAll, WaitMinSecond);
  2402. if Result then
  2403. begin
  2404. T := Now;
  2405. FWaitSecond := WaitMinSecond div 1000;
  2406. FWaitMinSecond := WaitMinSecond mod 1000;
  2407. while DirectoryExists(Source) do
  2408. begin
  2409. if T + EncodeTime(0, 0, FWaitSecond, FWaitMinSecond) < now then
  2410. begin
  2411. break;
  2412. end;
  2413. Sleep(1);
  2414. end;
  2415. end;
  2416. end;
  2417. function GetFileNamesFromDirectory(const DirName: string; const SearchFilter: string = '*';
  2418. const FileAttribs: Integer = faAnyFile; const isIncludeSubDirName: Boolean = False; const Recursion: Boolean = False;
  2419. const FullName: Boolean = False): string;
  2420. var
  2421. SubList, FileNameList: TStrings;
  2422. DirPath: string;
  2423. SearchRec: TSearchRec;
  2424. i: Integer;
  2425. begin
  2426. Result := '';
  2427. if Trim(SearchFilter) = '' then
  2428. begin
  2429. exit;
  2430. end;
  2431. if DirectoryExists(DirName) then
  2432. begin
  2433. FileNameList := TStringList.Create;
  2434. try
  2435. try
  2436. DirPath := IncludeTrailingPathDelimiter(DirName);
  2437. // 得到该目录下指定类型文件的文件名
  2438. if System.SysUtils.FindFirst(DirPath + Trim(SearchFilter), FileAttribs, SearchRec) = 0 then
  2439. try
  2440. repeat
  2441. if isIncludeSubDirName or ((SearchRec.Attr and faDirectory) <> faDirectory) then
  2442. begin
  2443. if (SearchRec.Attr and faDirectory) = faDirectory then
  2444. begin
  2445. if (ExtractFileName(SearchRec.Name) = '.') or (ExtractFileName(SearchRec.Name) = '..') then
  2446. begin
  2447. Continue;
  2448. end;
  2449. end;
  2450. if FullName then
  2451. begin
  2452. FileNameList.Add(ExpandFileName(SearchRec.Name));
  2453. end
  2454. else
  2455. begin
  2456. FileNameList.Add(ExtractFileName(SearchRec.Name));
  2457. end;
  2458. end;
  2459. until System.SysUtils.FindNext(SearchRec) <> 0;
  2460. finally
  2461. System.SysUtils.FindClose(SearchRec);
  2462. end;
  2463. // 递归该目录下所有子目录下的指定文件。
  2464. if Recursion and (System.SysUtils.FindFirst(DirPath + '*', faDirectory, SearchRec) = 0) then
  2465. try
  2466. repeat
  2467. if Recursion and ((SearchRec.Attr and faDirectory) = faDirectory) and
  2468. (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
  2469. begin
  2470. Result := GetFileNamesFromDirectory(DirPath + ExtractFileName(SearchRec.Name), SearchFilter,
  2471. FileAttribs, isIncludeSubDirName, Recursion, FullName);
  2472. SubList := TStringList.Create;
  2473. try
  2474. SubList.Clear;
  2475. SubList.Text := Result;
  2476. if not FullName then
  2477. begin
  2478. for i := 0 to SubList.Count - 1 do
  2479. begin
  2480. SubList.Strings[i] := ExtractFileName(SearchRec.Name) + PathDelim + SubList.Strings[i];
  2481. end;
  2482. end;
  2483. Result := '';
  2484. FileNameList.AddStrings(SubList);
  2485. finally
  2486. FreeAndNil(SubList);
  2487. end;
  2488. end;
  2489. until System.SysUtils.FindNext(SearchRec) <> 0;
  2490. finally
  2491. System.SysUtils.FindClose(SearchRec);
  2492. end;
  2493. Result := FileNameList.Text;
  2494. except
  2495. Result := '';
  2496. end;
  2497. finally
  2498. if FileNameList <> nil then
  2499. begin
  2500. FreeAndNil(FileNameList);
  2501. end;
  2502. end;
  2503. end;
  2504. end;
  2505. function GetCaseSensitiveFileName(const FileName: string; RootPath: string = ''): string;
  2506. var
  2507. MatchFound: TFilenameCaseMatch;
  2508. begin
  2509. Result := ExpandFileNameCase2(FileName, RootPath, MatchFound);
  2510. end;
  2511. function GetSDCardPath(Index: Integer = 0): string;
  2512. begin
  2513. Result := FindSDCardSubPath('', Index);
  2514. end;
  2515. function FindSDCardSubPath(SubPath: string; Index: Integer = 0): string;
  2516. var
  2517. PathDelimedSubPath: string;
  2518. UnPathDelimedSubPath: string;
  2519. {$IFDEF ANDROID}
  2520. VolumePathList: TStrings;
  2521. UsbIndex,
  2522. CdRomIndex,
  2523. TempVolumeIndex,
  2524. I: Integer;
  2525. IsFoundDir0: Boolean;
  2526. {$ENDIF ANDROID}
  2527. begin
  2528. PathDelimedSubPath := '';
  2529. UnPathDelimedSubPath := SubPath;
  2530. if UnPathDelimedSubPath <> '' then
  2531. begin
  2532. if UnPathDelimedSubPath.Chars[0] = PathDelim then
  2533. begin
  2534. UnPathDelimedSubPath := UnPathDelimedSubPath.Substring(1);
  2535. end;
  2536. PathDelimedSubPath := PathDelim + UnPathDelimedSubPath;
  2537. end;
  2538. {$IFDEF ANDROID}
  2539. Result := '/storage/emulated/' + IntToStr(Index) + PathDelimedSubPath;
  2540. if FVolumePaths.Trim = '' then GetVolumePaths;
  2541. VolumePathList := TStringList.Create;
  2542. try
  2543. VolumePathList.Text := FVolumePaths;
  2544. if Index = 0 then
  2545. begin
  2546. Result := JNIgetExternalStorageDirectory(UnPathDelimedSubPath);
  2547. if not DirectoryExists(Result) and (VolumePathList.Count > 0) then
  2548. begin
  2549. Result := VolumePathList[0];
  2550. end;
  2551. if not DirectoryExists(Result) then
  2552. begin
  2553. Result := '/storage/emulated/' + IntToStr(Index) + PathDelimedSubPath;
  2554. end;
  2555. if not DirectoryExists(Result) then
  2556. begin
  2557. Result := '/storage/sdcard0' + PathDelimedSubPath;
  2558. end;
  2559. if not DirectoryExists(Result) then
  2560. begin
  2561. Result := '/mnt/sdcard0' + PathDelimedSubPath;
  2562. end;
  2563. if not DirectoryExists(Result) then
  2564. begin
  2565. Result := '/storage/sdcard' + PathDelimedSubPath;
  2566. end;
  2567. if not DirectoryExists(Result) then
  2568. begin
  2569. Result := '/mnt/sdcard' + PathDelimedSubPath;
  2570. end;
  2571. if not DirectoryExists(Result) then
  2572. begin
  2573. Result := '/storage/sdcard_ext0' + PathDelimedSubPath;
  2574. end;
  2575. if not DirectoryExists(Result) then
  2576. begin
  2577. Result := '/storage/sdcard_ext' + PathDelimedSubPath;
  2578. end;
  2579. if not DirectoryExists(Result) then
  2580. begin
  2581. Result := '/sdcard' + PathDelimedSubPath;
  2582. end;
  2583. if not DirectoryExists(Result) then
  2584. begin
  2585. Result := '/mnt/sdcard_ext0' + PathDelimedSubPath;
  2586. end;
  2587. if not DirectoryExists(Result) then
  2588. begin
  2589. Result := '/mnt/sdcard_ext' + PathDelimedSubPath;
  2590. end;
  2591. if not DirectoryExists(Result) then
  2592. begin
  2593. Result := '/emmc' + PathDelimedSubPath;
  2594. end;
  2595. if not DirectoryExists(Result) then
  2596. begin
  2597. Result := '/nand' + PathDelimedSubPath;
  2598. end;
  2599. if not DirectoryExists(Result) then
  2600. begin
  2601. Result := '/flash' + PathDelimedSubPath;
  2602. end;
  2603. if not DirectoryExists(Result) then
  2604. begin
  2605. Result := '/mnt/emmc' + PathDelimedSubPath;
  2606. end;
  2607. if not DirectoryExists(Result) then
  2608. begin
  2609. Result := '/mnt/flash' + PathDelimedSubPath;
  2610. end;
  2611. if not DirectoryExists(Result) then
  2612. begin
  2613. Result := '/storage/flash' + PathDelimedSubPath;
  2614. end;
  2615. if not DirectoryExists(Result) then
  2616. begin
  2617. Result := '/mnt/D' + PathDelimedSubPath;
  2618. end;
  2619. if not DirectoryExists(Result) then
  2620. begin
  2621. Result := '/storage/D' + PathDelimedSubPath;
  2622. end;
  2623. if not DirectoryExists(Result) then
  2624. begin
  2625. Result := '/mnt/flash' + PathDelimedSubPath;
  2626. end;
  2627. if not DirectoryExists(Result) then
  2628. begin
  2629. Result := '/storage/flash' + PathDelimedSubPath;
  2630. end;
  2631. //last
  2632. if not DirectoryExists(Result) then
  2633. begin
  2634. Result := '/storage/sdcard0' + PathDelimedSubPath;
  2635. end;
  2636. end
  2637. else if (Index < UsbDiskStartIndex) then
  2638. begin
  2639. if not DirectoryExists(Result) then
  2640. begin
  2641. Result := '/storage/sdcard' + IntToStr(Index) + PathDelimedSubPath;
  2642. end;
  2643. if not DirectoryExists(Result) then
  2644. begin
  2645. Result := '/storage/sdcard' + IntToStr(Index + 1) + PathDelimedSubPath;
  2646. end;
  2647. if not DirectoryExists(Result) then
  2648. begin
  2649. Result := 'sdcard' + IntToStr(Index) + PathDelimedSubPath;
  2650. end;
  2651. if not DirectoryExists(Result) then
  2652. begin
  2653. Result := 'sdcard' + IntToStr(Index + 1) + PathDelimedSubPath;
  2654. end;
  2655. if not DirectoryExists(Result) then
  2656. begin
  2657. Result := '/storage/sdcard_ext' + IntToStr(Index) + PathDelimedSubPath;
  2658. end;
  2659. if not DirectoryExists(Result) then
  2660. begin
  2661. Result := '/storage/sdcard_ext' + IntToStr(Index + 1) + PathDelimedSubPath;
  2662. end;
  2663. if not DirectoryExists(Result) then
  2664. begin
  2665. Result := '/mnt/sdcard' + IntToStr(Index) + PathDelimedSubPath;
  2666. end;
  2667. if not DirectoryExists(Result) then
  2668. begin
  2669. Result := '/mnt/sdcard' + IntToStr(Index + 1) + PathDelimedSubPath;
  2670. end;
  2671. if not DirectoryExists(Result) then
  2672. begin
  2673. Result := '/mnt/sdcard_ext' + IntToStr(Index) + PathDelimedSubPath;
  2674. end;
  2675. if not DirectoryExists(Result) then
  2676. begin
  2677. Result := '/mnt/sdcard_ext' + IntToStr(Index + 1) + PathDelimedSubPath;
  2678. end;
  2679. if not DirectoryExists(Result) and (VolumePathList.Count > Index) then
  2680. begin
  2681. Result := VolumePathList[Index];
  2682. if (Result.IndexOf('usbcd') >= 0) or
  2683. (Result.IndexOf('usb') >= 0) or
  2684. (Result.IndexOf('otg') >= 0) or
  2685. (Result.IndexOf('-rw') >= 0) or
  2686. (Result.IndexOf('_rw') >= 0) or
  2687. (Result.IndexOf('cdrom') >= 0) or
  2688. (Result.IndexOf('cd_rom') >= 0) or
  2689. (Result.IndexOf('cd-rom') >= 0) then
  2690. begin
  2691. Result := '';
  2692. end;
  2693. end;
  2694. if Index = 1 then
  2695. begin
  2696. if not DirectoryExists(Result) and (VolumePathList.Count = 2) then
  2697. begin
  2698. Result := VolumePathList[1];
  2699. if (Result.IndexOf('/storage/emulated/') < 0) and
  2700. (Result.IndexOf('/storage/sdcard') < 0) and
  2701. (Result.IndexOf('/mnt/sdcard') < 0) then
  2702. begin
  2703. Result := '';
  2704. end;
  2705. end;
  2706. if not DirectoryExists(Result) then
  2707. begin
  2708. Result := '/storage/extSdCard' + PathDelimedSubPath;
  2709. end;
  2710. if not DirectoryExists(Result) then
  2711. begin
  2712. Result := '/storage/external1' + PathDelimedSubPath;
  2713. end;
  2714. if not DirectoryExists(Result) then
  2715. begin
  2716. Result := '/storage/sdcard-ext' + PathDelimedSubPath;
  2717. end;
  2718. if not DirectoryExists(Result) then
  2719. begin
  2720. Result := '/storage/ext_card' + PathDelimedSubPath;
  2721. end;
  2722. if not DirectoryExists(Result) then
  2723. begin
  2724. Result := '/storage/extsd' + PathDelimedSubPath;
  2725. end;
  2726. if not DirectoryExists(Result) then
  2727. begin
  2728. Result := '/storage/_ExternalSD' + PathDelimedSubPath;
  2729. end;
  2730. if not DirectoryExists(Result) then
  2731. begin
  2732. Result := '/storage/external_sd' + PathDelimedSubPath;
  2733. end;
  2734. if not DirectoryExists(Result) then
  2735. begin
  2736. Result := '/storage/removable_sdcard' + PathDelimedSubPath;
  2737. end;
  2738. if not DirectoryExists(Result) then
  2739. begin
  2740. Result := '/mnt/extSdCard' + PathDelimedSubPath;
  2741. end;
  2742. if not DirectoryExists(Result) then
  2743. begin
  2744. Result := '/mnt/external1' + PathDelimedSubPath;
  2745. end;
  2746. if not DirectoryExists(Result) then
  2747. begin
  2748. Result := '/mnt/sdcard-ext' + PathDelimedSubPath;
  2749. end;
  2750. if not DirectoryExists(Result) then
  2751. begin
  2752. Result := '/mnt/ext_card' + PathDelimedSubPath;
  2753. end;
  2754. if not DirectoryExists(Result) then
  2755. begin
  2756. Result := '/mnt/extsd' + PathDelimedSubPath;
  2757. end;
  2758. if not DirectoryExists(Result) then
  2759. begin
  2760. Result := '/mnt/_ExternalSD' + PathDelimedSubPath;
  2761. end;
  2762. if not DirectoryExists(Result) then
  2763. begin
  2764. Result := '/mnt/external_sd' + PathDelimedSubPath;
  2765. end;
  2766. if not DirectoryExists(Result) then
  2767. begin
  2768. Result := '/mnt/removable_sdcard' + PathDelimedSubPath;
  2769. end;
  2770. if not DirectoryExists(Result) then
  2771. begin
  2772. Result := 'tflash' + PathDelimedSubPath;
  2773. end;
  2774. if not DirectoryExists(Result) then
  2775. begin
  2776. Result := 'removable_sdcard' + PathDelimedSubPath;
  2777. end;
  2778. if not DirectoryExists(Result) then
  2779. begin
  2780. Result := GetSDCardPath(0) + 'tflash' + PathDelimedSubPath;
  2781. end;
  2782. if not DirectoryExists(Result) then
  2783. begin
  2784. Result := GetSDCardPath(0) + 'extSdCard' + PathDelimedSubPath;
  2785. end;
  2786. if not DirectoryExists(Result) then
  2787. begin
  2788. Result := GetSDCardPath(0) + 'external1' + PathDelimedSubPath;
  2789. end;
  2790. if not DirectoryExists(Result) then
  2791. begin
  2792. Result := GetSDCardPath(0) + 'sdcard-ext' + PathDelimedSubPath;
  2793. end;
  2794. if not DirectoryExists(Result) then
  2795. begin
  2796. Result := GetSDCardPath(0) + 'ext_card' + PathDelimedSubPath;
  2797. end;
  2798. if not DirectoryExists(Result) then
  2799. begin
  2800. Result := GetSDCardPath(0) + 'extsd' + PathDelimedSubPath;
  2801. end;
  2802. if not DirectoryExists(Result) then
  2803. begin
  2804. Result := GetSDCardPath(0) + '_ExternalSD' + PathDelimedSubPath;
  2805. end;
  2806. if not DirectoryExists(Result) then
  2807. begin
  2808. Result := GetSDCardPath(0) + 'external_sd' + PathDelimedSubPath;
  2809. end;
  2810. if not DirectoryExists(Result) then
  2811. begin
  2812. Result := GetSDCardPath(0) + 'removable_sdcard' + PathDelimedSubPath;
  2813. end;
  2814. end;
  2815. //last
  2816. if not DirectoryExists(Result) then
  2817. begin
  2818. Result := '/storage/sdcard' + IntToStr(Index) + PathDelimedSubPath;
  2819. end;
  2820. end
  2821. else if (Index >= UsbDiskStartIndex) and (Index < UsbDiskStartIndex + OTGDeivceCount) then //UsbDiskStartIndex
  2822. begin
  2823. UsbIndex := Index - UsbDiskStartIndex;
  2824. TempVolumeIndex := UsbIndex;
  2825. Result := '';
  2826. for I := 1 to VolumePathList.Count - 1 do
  2827. begin
  2828. Result := ExcludeTrailingPathDelimiter(VolumePathList[I]);
  2829. if (Result.IndexOf('/storage/usb') < 0) and
  2830. (Result.IndexOf('/storage/otg') < 0) and
  2831. (Result.IndexOf('/mnt/otg') < 0) and
  2832. (Result.IndexOf('/mnt/usb') < 0) then
  2833. begin
  2834. Result := '';
  2835. end
  2836. else if DirectoryExists(Result) then
  2837. begin
  2838. if (Result.IndexOf('usbcd') >= 0) then
  2839. begin
  2840. Result := '';
  2841. end
  2842. else if (Result.IndexOf('otgcd') >= 0) then
  2843. begin
  2844. Result := '';
  2845. end
  2846. else if (TempVolumeIndex = 0) and (Result.Chars[Result.Length] in ['2'..'9']) then
  2847. begin
  2848. Result := '';
  2849. end
  2850. else if (TempVolumeIndex <> 0) and (Result.Chars[Result.Length] <> IntToStr(TempVolumeIndex)) then
  2851. begin
  2852. Result := '';
  2853. end
  2854. else
  2855. begin
  2856. Result := Result + PathDelimedSubPath;
  2857. break;
  2858. end;
  2859. end;
  2860. end;
  2861. if (Result = '') or (not DirectoryExists(Result)) then
  2862. begin
  2863. if UsbIndex = 0 then
  2864. begin
  2865. Result := '/storage/udisk' + PathDelimedSubPath;
  2866. if not DirectoryExists(Result) then
  2867. begin
  2868. Result := '/mnt/udisk' + PathDelimedSubPath;
  2869. end;
  2870. if not DirectoryExists(Result) then
  2871. begin
  2872. Result := 'udisk' + PathDelimedSubPath;
  2873. end;
  2874. if not DirectoryExists(Result) then
  2875. begin
  2876. Result := '/storage/usbdisk' + PathDelimedSubPath;
  2877. end;
  2878. if not DirectoryExists(Result) then
  2879. begin
  2880. Result := '/mnt/usbdisk' + PathDelimedSubPath;
  2881. end;
  2882. if not DirectoryExists(Result) then
  2883. begin
  2884. Result := 'usbdisk' + PathDelimedSubPath;
  2885. end;
  2886. if not DirectoryExists(Result) then
  2887. begin
  2888. Result := '/storage/usbotg' + PathDelimedSubPath;
  2889. end;
  2890. if not DirectoryExists(Result) then
  2891. begin
  2892. Result := '/mnt/usbotg' + PathDelimedSubPath;
  2893. end;
  2894. if not DirectoryExists(Result) then
  2895. begin
  2896. Result := 'usbotg' + PathDelimedSubPath;
  2897. end;
  2898. if not DirectoryExists(Result) then
  2899. begin
  2900. Result := '/storage/usbdrive' + PathDelimedSubPath;
  2901. end;
  2902. if not DirectoryExists(Result) then
  2903. begin
  2904. Result := '/mnt/usbdrive' + PathDelimedSubPath;
  2905. end;
  2906. if not DirectoryExists(Result) then
  2907. begin
  2908. Result := 'usbdrive' + PathDelimedSubPath;
  2909. end;
  2910. if not DirectoryExists(Result) then
  2911. begin
  2912. Result := '/storage/usb' + PathDelimedSubPath;
  2913. end;
  2914. if not DirectoryExists(Result) then
  2915. begin
  2916. Result := '/mnt/usb' + PathDelimedSubPath;
  2917. end;
  2918. if not DirectoryExists(Result) then
  2919. begin
  2920. Result := 'usb' + PathDelimedSubPath;
  2921. end;
  2922. if not DirectoryExists(Result) then
  2923. begin
  2924. Result := '/storage/otg' + PathDelimedSubPath;
  2925. end;
  2926. if not DirectoryExists(Result) then
  2927. begin
  2928. Result := '/mnt/otg' + PathDelimedSubPath;
  2929. end;
  2930. if not DirectoryExists(Result) then
  2931. begin
  2932. Result := 'otg' + PathDelimedSubPath;
  2933. end;
  2934. if not DirectoryExists(Result) then
  2935. begin
  2936. Result := GetSDCardPath(0) + 'udisk' + PathDelimedSubPath;
  2937. end;
  2938. if not DirectoryExists(Result) then
  2939. begin
  2940. Result := GetSDCardPath(0) + 'usbotg' + PathDelimedSubPath;
  2941. end;
  2942. if not DirectoryExists(Result) then
  2943. begin
  2944. Result := GetSDCardPath(0) + 'usb' + PathDelimedSubPath;
  2945. end;
  2946. if not DirectoryExists(Result) then
  2947. begin
  2948. Result := GetSDCardPath(0) + 'otg' + PathDelimedSubPath;
  2949. end;
  2950. //index = 0
  2951. if not DirectoryExists(Result) then
  2952. begin
  2953. Result := '/storage/udisk0' + PathDelimedSubPath;
  2954. end;
  2955. if not DirectoryExists(Result) then
  2956. begin
  2957. Result := '/mnt/udisk0' + PathDelimedSubPath;
  2958. end;
  2959. if not DirectoryExists(Result) then
  2960. begin
  2961. Result := 'udisk0' + PathDelimedSubPath;
  2962. end;
  2963. if not DirectoryExists(Result) then
  2964. begin
  2965. Result := '/storage/usbdisk0' + PathDelimedSubPath;
  2966. end;
  2967. if not DirectoryExists(Result) then
  2968. begin
  2969. Result := '/mnt/usbdisk0' + PathDelimedSubPath;
  2970. end;
  2971. if not DirectoryExists(Result) then
  2972. begin
  2973. Result := 'usbdisk0' + PathDelimedSubPath;
  2974. end;
  2975. if not DirectoryExists(Result) then
  2976. begin
  2977. Result := '/storage/usbotg0' + PathDelimedSubPath;
  2978. end;
  2979. if not DirectoryExists(Result) then
  2980. begin
  2981. Result := '/mnt/usbotg0' + PathDelimedSubPath;
  2982. end;
  2983. if not DirectoryExists(Result) then
  2984. begin
  2985. Result := 'usbotg0' + PathDelimedSubPath;
  2986. end;
  2987. if not DirectoryExists(Result) then
  2988. begin
  2989. Result := '/storage/usbdrive0' + PathDelimedSubPath;
  2990. end;
  2991. if not DirectoryExists(Result) then
  2992. begin
  2993. Result := '/mnt/usbdrive0' + PathDelimedSubPath;
  2994. end;
  2995. if not DirectoryExists(Result) then
  2996. begin
  2997. Result := 'usbdrive0' + PathDelimedSubPath;
  2998. end;
  2999. if not DirectoryExists(Result) then
  3000. begin
  3001. Result := '/storage/usb0' + PathDelimedSubPath;
  3002. end;
  3003. if not DirectoryExists(Result) then
  3004. begin
  3005. Result := '/mnt/usb0' + PathDelimedSubPath;
  3006. end;
  3007. if not DirectoryExists(Result) then
  3008. begin
  3009. Result := 'usb0' + PathDelimedSubPath;
  3010. end;
  3011. if not DirectoryExists(Result) then
  3012. begin
  3013. Result := GetSDCardPath(0) + 'udisk0' + PathDelimedSubPath;
  3014. end;
  3015. if not DirectoryExists(Result) then
  3016. begin
  3017. Result := GetSDCardPath(0) + 'usbotg0' + PathDelimedSubPath;
  3018. end;
  3019. if not DirectoryExists(Result) then
  3020. begin
  3021. Result := GetSDCardPath(0) + 'usb0' + PathDelimedSubPath;
  3022. end;
  3023. end
  3024. else
  3025. begin
  3026. //index > 0
  3027. IsFoundDir0 := False;
  3028. Result := GetSDCardPath(UsbDiskStartIndex);
  3029. IsFoundDir0 := Result.Chars[Result.Length] = '0';
  3030. if not IsFoundDir0 then
  3031. begin
  3032. inc(TempVolumeIndex);
  3033. end;
  3034. Result := '/storage/udisk' + IntToStr(TempVolumeIndex) + PathDelimedSubPath;
  3035. if not DirectoryExists(Result) then
  3036. begin
  3037. Result := '/mnt/udisk' + IntToStr(TempVolumeIndex) + PathDelimedSubPath;
  3038. end;
  3039. if not DirectoryExists(Result) then
  3040. begin
  3041. Result := 'udisk' + IntToStr(TempVolumeIndex) + PathDelimedSubPath;
  3042. end;
  3043. if not DirectoryExists(Result) then
  3044. begin
  3045. Result := '/storage/usbdisk' + IntToStr(TempVolumeIndex) + PathDelimedSubPath;
  3046. end;
  3047. if not DirectoryExists(Result) then
  3048. begin
  3049. Result := '/mnt/usbdisk' + IntToStr(TempVolumeIndex) + PathDelimedSubPath;
  3050. end;
  3051. if not DirectoryExists(Result) then
  3052. begin
  3053. Result := 'usbdisk' + IntToStr(TempVolumeIndex) + PathDelimedSubPath;
  3054. end;
  3055. if not DirectoryExists(Result) then
  3056. begin
  3057. Result := '/storage/usbotg' + IntToStr(TempVolumeIndex) + PathDelimedSubPath;
  3058. end;
  3059. if not DirectoryExists(Result) then
  3060. begin
  3061. Result := '/mnt/usbotg' + IntToStr(TempVolumeIndex) + PathDelimedSubPath;
  3062. end;
  3063. if not DirectoryExists(Result) then
  3064. begin
  3065. Result := 'usbotg' + IntToStr(TempVolumeIndex) + PathDelimedSubPath;
  3066. end;
  3067. if not DirectoryExists(Result) then
  3068. begin
  3069. Result := '/storage/usbdrive' + IntToStr(TempVolumeIndex) + PathDelimedSubPath;
  3070. end;
  3071. if not DirectoryExists(Result) then
  3072. begin
  3073. Result := '/mnt/usbdrive' + IntToStr(TempVolumeIndex) + PathDelimedSubPath;
  3074. end;
  3075. if not DirectoryExists(Result) then
  3076. begin
  3077. Result := 'usbdrive' + IntToStr(TempVolumeIndex) + PathDelimedSubPath;
  3078. end;
  3079. if not DirectoryExists(Result) then
  3080. begin
  3081. Result := '/storage/usb' + IntToStr(TempVolumeIndex) + PathDelimedSubPath;
  3082. end;
  3083. if not DirectoryExists(Result) then
  3084. begin
  3085. Result := '/mnt/usb' + IntToStr(TempVolumeIndex) + PathDelimedSubPath;
  3086. end;
  3087. if not DirectoryExists(Result) then
  3088. begin
  3089. Result := 'usb' + IntToStr(TempVolumeIndex) + PathDelimedSubPath;
  3090. end;
  3091. if not DirectoryExists(Result) then
  3092. begin
  3093. Result := '/storage/otg' + IntToStr(TempVolumeIndex) + PathDelimedSubPath;
  3094. end;
  3095. if not DirectoryExists(Result) then
  3096. begin
  3097. Result := '/mnt/otg' + IntToStr(TempVolumeIndex) + PathDelimedSubPath;
  3098. end;
  3099. if not DirectoryExists(Result) then
  3100. begin
  3101. Result := 'otg' + IntToStr(TempVolumeIndex) + PathDelimedSubPath;
  3102. end;
  3103. if not DirectoryExists(Result) then
  3104. begin
  3105. Result := GetSDCardPath(0) + 'udisk' + IntToStr(TempVolumeIndex) + PathDelimedSubPath;
  3106. end;
  3107. if not DirectoryExists(Result) then
  3108. begin
  3109. Result := GetSDCardPath(0) + 'usbotg' + IntToStr(TempVolumeIndex) + PathDelimedSubPath;
  3110. end;
  3111. if not DirectoryExists(Result) then
  3112. begin
  3113. Result := GetSDCardPath(0) + 'usb' + IntToStr(TempVolumeIndex) + PathDelimedSubPath;
  3114. end;
  3115. if not DirectoryExists(Result) then
  3116. begin
  3117. Result := GetSDCardPath(0) + 'otg' + IntToStr(TempVolumeIndex) + PathDelimedSubPath;
  3118. end;
  3119. end;
  3120. end;
  3121. //last
  3122. if (Result = '') or (not DirectoryExists(Result)) then
  3123. begin
  3124. if UsbIndex = 0 then
  3125. begin
  3126. Result := '/storage/udisk' + PathDelimedSubPath;
  3127. end
  3128. else
  3129. begin
  3130. Result := '/storage/udisk' + IntToStr(TempVolumeIndex) + PathDelimedSubPath;
  3131. end;
  3132. end;
  3133. end
  3134. else if (Index >= CDROMStartIndex) and (Index < CDROMStartIndex + OTGDeivceCount) then //CDROMStartIndex
  3135. begin
  3136. CdRomIndex := Index - CDROMStartIndex;
  3137. TempVolumeIndex := CdRomIndex;
  3138. for I := 1 to VolumePathList.Count - 1 do
  3139. begin
  3140. Result := ExcludeTrailingPathDelimiter(VolumePathList[I]);
  3141. if (Result.IndexOf('/storage/cd') < 0) and
  3142. (Result.IndexOf('/mnt/cd') < 0) and
  3143. (Result.IndexOf('/storage/usbcd') < 0) and
  3144. (Result.IndexOf('/mnt/usbcd') < 0) then
  3145. begin
  3146. Result := '';
  3147. end
  3148. else if DirectoryExists(Result) then
  3149. begin
  3150. if (TempVolumeIndex = 0) and (Result.Chars[Result.Length] in ['2'..'9']) then
  3151. begin
  3152. Result := '';
  3153. end
  3154. else if (TempVolumeIndex <> 0) and (Result.Chars[Result.Length] <> IntToStr(TempVolumeIndex)) then
  3155. begin
  3156. Result := '';
  3157. end
  3158. else
  3159. begin
  3160. Result := Result + PathDelimedSubPath;
  3161. break;
  3162. end;
  3163. end;
  3164. end;
  3165. if (Result = '') or (not DirectoryExists(Result)) then
  3166. begin
  3167. if CdRomIndex = 0 then
  3168. begin
  3169. Result := '/storage/cd-rom' + PathDelimedSubPath;
  3170. if not DirectoryExists(Result) then
  3171. begin
  3172. Result := '/mnt/cd-rom' + PathDelimedSubPath;
  3173. end;
  3174. if not DirectoryExists(Result) then
  3175. begin
  3176. Result := 'cd-rom' + PathDelimedSubPath;
  3177. end;
  3178. Result := '/storage/media_rw' + PathDelimedSubPath;
  3179. if not DirectoryExists(Result) then
  3180. begin
  3181. Result := '/mnt/media_rw' + PathDelimedSubPath;
  3182. end;
  3183. if not DirectoryExists(Result) then
  3184. begin
  3185. Result := 'media_rw' + PathDelimedSubPath;
  3186. end;
  3187. Result := '/storage/cd_rom' + PathDelimedSubPath;
  3188. if not DirectoryExists(Result) then
  3189. begin
  3190. Result := '/mnt/cd_rom' + PathDelimedSubPath;
  3191. end;
  3192. if not DirectoryExists(Result) then
  3193. begin
  3194. Result := 'cd_rom' + PathDelimedSubPath;
  3195. end;
  3196. if not DirectoryExists(Result) then
  3197. begin
  3198. Result := '/storage/usbcdrom' + PathDelimedSubPath;
  3199. end;
  3200. if not DirectoryExists(Result) then
  3201. begin
  3202. Result := '/mnt/usbcdrom' + PathDelimedSubPath;
  3203. end;
  3204. if not DirectoryExists(Result) then
  3205. begin
  3206. Result := 'usbcdrom' + PathDelimedSubPath;
  3207. end;
  3208. if not DirectoryExists(Result) then
  3209. begin
  3210. Result := '/storage/cdrom' + PathDelimedSubPath;
  3211. end;
  3212. if not DirectoryExists(Result) then
  3213. begin
  3214. Result := '/mnt/cdrom' + PathDelimedSubPath;
  3215. end;
  3216. if not DirectoryExists(Result) then
  3217. begin
  3218. Result := 'cdrom' + PathDelimedSubPath;
  3219. end;
  3220. if not DirectoryExists(Result) then
  3221. begin
  3222. Result := GetSDCardPath(0) + 'cd-rom' + PathDelimedSubPath;
  3223. end;
  3224. if not DirectoryExists(Result) then
  3225. begin
  3226. Result := GetSDCardPath(0) + 'usbcdrom' + PathDelimedSubPath;
  3227. end;
  3228. if not DirectoryExists(Result) then
  3229. begin
  3230. Result := GetSDCardPath(0) + 'cdrom' + PathDelimedSubPath;
  3231. end;
  3232. Result := '/storage/cd-rom' + PathDelimedSubPath;
  3233. if not DirectoryExists(Result) then
  3234. begin
  3235. Result := '/mnt/cd-rom' + PathDelimedSubPath;
  3236. end;
  3237. if not DirectoryExists(Result) then
  3238. begin
  3239. Result := 'cd-rom' + PathDelimedSubPath;
  3240. end;
  3241. //index = 0
  3242. if not DirectoryExists(Result) then
  3243. begin
  3244. Result := '/storage/cd_rom0' + PathDelimedSubPath;
  3245. end;
  3246. if not DirectoryExists(Result) then
  3247. begin
  3248. Result := '/mnt/cd_rom0' + PathDelimedSubPath;
  3249. end;
  3250. if not DirectoryExists(Result) then
  3251. begin
  3252. Result := 'cd_rom0' + PathDelimedSubPath;
  3253. end;
  3254. if not DirectoryExists(Result) then
  3255. begin
  3256. Result := '/storage/usbcdrom0' + PathDelimedSubPath;
  3257. end;
  3258. if not DirectoryExists(Result) then
  3259. begin
  3260. Result := '/mnt/usbcdrom0' + PathDelimedSubPath;
  3261. end;
  3262. if not DirectoryExists(Result) then
  3263. begin
  3264. Result := 'usbcdrom0' + PathDelimedSubPath;
  3265. end;
  3266. if not DirectoryExists(Result) then
  3267. begin
  3268. Result := '/storage/cdrom0' + PathDelimedSubPath;
  3269. end;
  3270. if not DirectoryExists(Result) then
  3271. begin
  3272. Result := '/mnt/cdrom0' + PathDelimedSubPath;
  3273. end;
  3274. if not DirectoryExists(Result) then
  3275. begin
  3276. Result := 'cdrom0' + PathDelimedSubPath;
  3277. end;
  3278. if not DirectoryExists(Result) then
  3279. begin
  3280. Result := GetSDCardPath(0) + 'cd-rom0' + PathDelimedSubPath;
  3281. end;
  3282. if not DirectoryExists(Result) then
  3283. begin
  3284. Result := GetSDCardPath(0) + 'usbcdrom0' + PathDelimedSubPath;
  3285. end;
  3286. if not DirectoryExists(Result) then
  3287. begin
  3288. Result := GetSDCardPath(0) + 'cdrom0' + PathDelimedSubPath;
  3289. end;
  3290. end
  3291. else
  3292. begin
  3293. //index > 0
  3294. IsFoundDir0 := False;
  3295. Result := GetSDCardPath(CDROMStartIndex);
  3296. IsFoundDir0 := Result.Chars[Result.Length] = '0';
  3297. if not IsFoundDir0 then
  3298. begin
  3299. inc(TempVolumeIndex);
  3300. end;
  3301. Result := '/storage/cd_rom' + IntToStr(TempVolumeIndex) + PathDelimedSubPath;
  3302. if not DirectoryExists(Result) then
  3303. begin
  3304. Result := '/mnt/cd_rom' + IntToStr(TempVolumeIndex) + PathDelimedSubPath;
  3305. end;
  3306. if not DirectoryExists(Result) then
  3307. begin
  3308. Result := 'cd_rom' + IntToStr(TempVolumeIndex) + PathDelimedSubPath;
  3309. end;
  3310. if not DirectoryExists(Result) then
  3311. begin
  3312. Result := '/storage/usbcdrom' + IntToStr(TempVolumeIndex) + PathDelimedSubPath;
  3313. end;
  3314. if not DirectoryExists(Result) then
  3315. begin
  3316. Result := '/mnt/usbcdrom' + IntToStr(TempVolumeIndex) + PathDelimedSubPath;
  3317. end;
  3318. if not DirectoryExists(Result) then
  3319. begin
  3320. Result := 'usbcdrom' + IntToStr(TempVolumeIndex) + PathDelimedSubPath;
  3321. end;
  3322. if not DirectoryExists(Result) then
  3323. begin
  3324. Result := '/storage/cdrom' + IntToStr(TempVolumeIndex) + PathDelimedSubPath;
  3325. end;
  3326. if not DirectoryExists(Result) then
  3327. begin
  3328. Result := '/mnt/cdrom' + IntToStr(TempVolumeIndex) + PathDelimedSubPath;
  3329. end;
  3330. if not DirectoryExists(Result) then
  3331. begin
  3332. Result := 'cdrom' + IntToStr(TempVolumeIndex) + PathDelimedSubPath;
  3333. end;
  3334. if not DirectoryExists(Result) then
  3335. begin
  3336. Result := GetSDCardPath(0) + 'cd-rom' + IntToStr(TempVolumeIndex) + PathDelimedSubPath;
  3337. end;
  3338. if not DirectoryExists(Result) then
  3339. begin
  3340. Result := GetSDCardPath(0) + 'usbcdrom' + IntToStr(TempVolumeIndex) + PathDelimedSubPath;
  3341. end;
  3342. if not DirectoryExists(Result) then
  3343. begin
  3344. Result := GetSDCardPath(0) + 'cdrom' + IntToStr(TempVolumeIndex) + PathDelimedSubPath;
  3345. end;
  3346. end;
  3347. end;
  3348. //last
  3349. if (Result = '') or (not DirectoryExists(Result)) then
  3350. begin
  3351. if CdRomIndex = 0 then
  3352. begin
  3353. Result := '/storage/cd-rom' + PathDelimedSubPath;
  3354. end
  3355. else
  3356. begin
  3357. Result := '/storage/cd-rom' + IntToStr(TempVolumeIndex) + PathDelimedSubPath;
  3358. end;
  3359. end;
  3360. end
  3361. else
  3362. begin
  3363. Result := JNIgetExternalStorageDirectory(UnPathDelimedSubPath);
  3364. end;
  3365. finally
  3366. FreeAndNil(VolumePathList);
  3367. end;
  3368. {$ELSE}
  3369. if UnPathDelimedSubPath <> '' then
  3370. begin
  3371. Result := IncludeTrailingPathDelimiter(
  3372. System.IOUtils.TPath.GetSharedDocumentsPath) + UnPathDelimedSubPath;
  3373. end
  3374. else
  3375. begin
  3376. Result := System.IOUtils.TPath.GetSharedDocumentsPath;
  3377. end;
  3378. {$ENDIF ANDROID}
  3379. Result := IncludeTrailingPathDelimiter(Result);
  3380. end;
  3381. function GetAppPath: string;
  3382. begin
  3383. {$IF Defined(ANDROID)}
  3384. Result := ExtractFilePath(
  3385. ExcludeTrailingPathDelimiter(
  3386. System.IOUtils.TPath.GetHomePath));
  3387. {$ELSE}
  3388. Result := ExtractFilePath(ParamStr(0));
  3389. {$ENDIF ANDROID}
  3390. Result := IncludeTrailingPathDelimiter(Result);
  3391. end;
  3392. //initialization
  3393. //
  3394. //finalization
  3395. // UnRegisterSDReceiver;
  3396. end.