package Ġcom . alis af dar . en oc assessment . data . managers Ċ Ċ import Ġandroid . Manifest Ċ import Ġandroid . app . Activity Ċ import Ġandroid . content . Intent Ċ import Ġandroid . net . Uri Ċ import Ġandroid . os . Build Ċ import Ġandroid . os . Environment Ċ import Ġandroid . provider . Media Store Ċ import Ġandroid . provider . Media Store . Files . File Columns Ċ import Ġandroid . provider . Media Store . Images . Media Ċ import Ġandroidx . core . content . File Provider Ċ import Ġcom . alis af dar . en oc assessment . data . constants . DIRECTORY _ NAME Ċ import Ġcom . alis af dar . en oc assessment . data . constants . REQUEST _ CODE _ IMAGE _ FROM _ G ALL ERY Ċ import Ġcom . alis af dar . en oc assessment . data . constants . REQUEST _ CODE _ TA KE _ PH OTO Ċ import Ġcom . alis af dar . en oc assessment . data . constants . REQUEST _ CODE _ VIDEO _ FROM _ G ALL ERY Ċ import Ġcom . alis af dar . en oc assessment . ui . dialogs . Photo Select Dialog Ċ import Ġcom . alis af dar . en oc assessment . utils . getFile Data From URI Ċ import Ġcom . alis af dar . en oc assessment . utils . is Device Support Camera Ċ import Ġcom . gr umpy sh oe . module . permission manager . impl . Permission ManagerImpl Ċ import Ġjava . io . File Ċ import Ġjava . text . SimpleDateFormat Ċ import Ġjava . util .* Ċ Ċ class ĠPhoto Manager ( act : ĠActivity ) Ġ{ Ċ ĉ var Ġactivity Ġ= Ġact Ċ ĉ private Ġvar Ġfile Uri : ĠUri ? Ġ= ĠUri . parse ("") ĊĊ ĉ fun Ġshow Photo Select Dialog () Ġ{ Ċĉ ĉ Photo Select Dialog ( activity , Ġtake Photo Ġ= Ġ{ Ġphoto Pressed () Ġ}, Ġselect Gallery Ġ= Ġ{ Ġopen Image Gallery () Ġ}). show () Ċ ĉ } ĊĊ ĉ fun Ġon Activity Result ( request Code : ĠInt , ĠresultCode : ĠInt , Ġdata : ĠIntent ?, Ġon Result : Ġ( content Uri : ĠUri ?, Ġfile Uri : ĠUri ?) Ġ-> ĠUnit ) Ġ{ Ċĉ ĉ if Ġ( result Code Ġ== ĠActivity . RESULT _ OK ) Ġ{ Ċĉĉ ĉ when Ġ( request Code ) Ġ{ Ċĉĉĉ ĉ REQUEST _ CODE _ IMAGE _ FROM _ G ALL ERY Ġ-> Ġif Ġ( data Ġ!= Ġnull ) Ġ{ Ċĉĉĉĉ ĉ val Ġcontent Uri Ġ= Ġdata . data Ċĉĉĉĉ ĉ file Uri Ġ= ĠUri . parse ( getFile Data From URI ( activity , Ġcontent Uri , ĠMedia . DATA )) Ċĉĉĉĉ ĉ activity . content Resolver . take Persist able Uri Permission ( content Uri Ġ!! , ĠIntent . FLAG _ GRANT _ READ _ URI _ PERMISSION ) ĊĊĉĉĉĉ ĉ on Result ( content Uri , Ġfile Uri ) Ċĉĉĉ ĉ } ĊĊĉĉĉ ĉ REQUEST _ CODE _ TA KE _ PH OTO Ġ-> Ġ{ Ċĉĉĉĉ ĉ val Ġfile Ġ= ĠFile ( file Uri . toString ()) Ċĉĉĉĉ ĉ val Ġcontent Uri Ġ= ĠFile Provider . get Uri For File ( activity , Ġactivity . packageName Ġ+ Ġ". provider ", Ġfile ) Ċĉĉĉĉ ĉ on Result ( content Uri , Ġfile Uri ) Ċĉĉĉ ĉ } Ċĉĉ ĉ } Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ private Ġfun Ġget Photo Gallery Intent (): ĠIntent Ġ{ Ċĉ ĉ val Ġintent Ġ= ĠIntent ( Intent . ACTION _ OPEN _ DOCUMENT ) Ċĉ ĉ intent . type Ġ= Ġ" image /* " Ċĉ ĉ intent . add Category ( Intent . CATEGORY _ OP ENABLE ) Ċĉ ĉ intent . type Ġ= Ġ" image /* " Ċĉ ĉ intent . putExtra ( Intent . EXTRA _ MIME _ TYPES , ĠarrayOf (" image /* ")) Ċĉ ĉ intent . putExtra ( Media Store . EXTRA _ SIZE _ LIMIT , Ġ 1 0 4 8 5 7 6 L ) Ċĉ ĉ intent . add Flags ( Intent . FLAG _ GRANT _ READ _ URI _ PERMISSION ) Ċĉ ĉ intent . flags Ġ= ĠIntent . FLAG _ GRANT _ READ _ URI _ PERMISSION Ċĉ ĉ // ĉ intent . add Category ( Intent . CATEGORY _ OP ENABLE ) Ċĉ ĉ // ĉ intent . putExtra ( Intent . EXTRA _ MIME _ TYPES , ĠarrayOf (" image /* ", Ġ" video /* ")) Ċĉ ĉ return Ġintent Ċ ĉ } ĊĊ ĉ private Ġfun Ġget Video Gallery Intent (): ĠIntent Ġ{ Ċĉ ĉ val Ġintent Ġ= ĠIntent ( Intent . ACTION _ OPEN _ DOCUMENT ) Ċĉ ĉ intent . type Ġ= Ġ" video /* " Ċĉ ĉ intent . add Category ( Intent . CATEGORY _ OP ENABLE ) Ċĉ ĉ intent . type Ġ= Ġ" video /* " Ċĉ ĉ intent . putExtra ( Intent . EXTRA _ MIME _ TYPES , ĠarrayOf (" video /* ")) Ċĉ ĉ intent . putExtra ( Media Store . EXTRA _ SIZE _ LIMIT , Ġ 1 0 4 8 5 7 6 L ) Ċĉ ĉ intent . add Flags ( Intent . FLAG _ GRANT _ READ _ URI _ PERMISSION ) Ċĉ ĉ intent . flags Ġ= ĠIntent . FLAG _ GRANT _ READ _ URI _ PERMISSION Ċĉ ĉ // ĉ intent . add Category ( Intent . CATEGORY _ OP ENABLE ) Ċĉ ĉ // ĉ intent . putExtra ( Intent . EXTRA _ MIME _ TYPES , ĠarrayOf (" image /* ", Ġ" video /* ")) Ċĉ ĉ return Ġintent Ċ ĉ } ĊĊ ĉ private Ġfun Ġget Camera Intent ( on Camera Intent : Ġ( Boolean , ĠIntent , ĠUri ) Ġ-> ĠUnit ) Ġ{ Ċĉ ĉ val Ġintent Ġ= ĠIntent ( Media Store . ACTION _ IMAGE _ CAPTURE ) Ċĉ ĉ var Ġfile Uri : ĠUri Ċĉ ĉ if Ġ( Build . VERSION . SDK _ INT Ġ< ĠBuild . VERSION _ CODES . N ) Ġ{ Ċĉĉ ĉ file Uri Ġ= Ġget Output Media File Uri ( File Columns . MEDIA _ TYPE _ IMAGE ) Ċĉĉ ĉ intent . putExtra ( Media Store . EXTRA _ OUTPUT , Ġfile Uri ) Ċĉ ĉ } Ġelse Ġ{ Ċĉĉ ĉ val Ġfile Ġ= ĠFile ( getOutput Media File Uri ( File Columns . MEDIA _ TYPE _ IMAGE ). path Ġ!! ) Ċĉĉ ĉ file Uri Ġ= ĠUri . parse ( file . path ) ĊĊĉĉ ĉ val Ġuri Ġ= ĠFile Provider . get Uri For File ( activity , Ġactivity . packageName Ġ+ Ġ". provider ", Ġfile ) Ċĉĉ ĉ intent . putExtra ( Media Store . EXTRA _ OUTPUT , Ġuri ) Ċĉ ĉ } Ċĉ ĉ intent . add Flags ( Intent . FLAG _ GRANT _ READ _ URI _ PERMISSION ) Ċĉ ĉ if Ġ( intent . resolve Activity ( activity . package Manager ) Ġ!= Ġnull ) Ġ{ Ċĉĉ ĉ on Camera Intent ( true , Ġintent , Ġfile Uri ) Ċĉ ĉ } Ġelse Ġ{ Ċĉĉ ĉ on Camera Intent ( false , Ġintent , Ġfile Uri ) Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ private Ġfun Ġget Output Media File Uri ( type : ĠInt ): ĠUri Ġ{ Ċĉ ĉ return ĠUri . from File ( getOutput Media File ( type )) Ċ ĉ } ĊĊ ĉ private Ġfun Ġget Output Media File ( type : ĠInt ): ĠFile ? Ġ{ ĉ ĉ // ĠExternal Ġsd card Ġlocation Ċĉ ĉ val Ġmedia Storage Dir Ġ= ĠFile ( activity . get External Files Dir ( Environment . DIRECTORY _ PI CTURE S ), ĠDIRECTORY _ NAME ) ĊĊĉ ĉ // ĠCreate Ġthe Ġstorage Ġdirectory Ġif Ġit Ġdoes Ġnot Ġexist Ċĉ ĉ if Ġ(! media Storage Dir . exists ()) Ġ{ Ċĉĉ ĉ if Ġ(! media Storage Dir . mkdir s ()) Ġ{ Ċĉĉĉ ĉ return Ġnull Ċĉĉ ĉ } Ċĉ ĉ } ĊĊĉ ĉ // ĠCreate Ġa Ġmedia Ġfile Ġname Ċĉ ĉ val Ġtime Stamp Ġ= ĠSimpleDateFormat (" yyyy MM dd _ HH mm ss ", ĠLocale . getDefault ()). format ( Date ()) ĊĊĉ ĉ return Ġwhen Ġ( type ) Ġ{ Ċĉĉ ĉ File Columns . MEDIA _ TYPE _ IMAGE Ġ-> Ġ{ Ċĉĉĉ ĉ File ( media Storage Dir . path Ġ+ ĠFile . separator Ġ+ Ġ" IMG _" Ġ+ Ġtime Stamp Ġ+ Ġ". jpg ") Ċĉĉ ĉ } Ċĉĉ ĉ File Columns . MEDIA _ TYPE _ VIDEO Ġ-> Ġ{ Ċĉĉĉ ĉ File ( media Storage Dir . path Ġ+ ĠFile . separator Ġ+ Ġ" VID _" Ġ+ Ġtime Stamp Ġ+ Ġ". mp 4 ") Ċĉĉ ĉ } Ċĉĉ ĉ else Ġ-> Ġ{ Ċĉĉĉ ĉ return Ġnull Ċĉĉ ĉ } Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ fun Ġphoto Pressed () Ġ{ Ċĉ ĉ if Ġ( give Camera Permission ( activity , Ġfalse )) Ġ{ Ċĉĉ ĉ take Photo Pressed () Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ private Ġfun Ġopen Image Gallery () Ġ{ Ċĉ ĉ if Ġ( give Camera Permission ( activity , Ġtrue )) Ġ{ Ċĉĉ ĉ select From Image Gallery () Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ fun Ġopen Video Gallery () Ġ{ Ċĉ ĉ if Ġ( give Camera Permission ( activity , Ġtrue )) Ġ{ Ċĉĉ ĉ select From Video Gallery () Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ private Ġfun Ġgive Camera Permission ( activity : ĠActivity , Ġis Gallery : ĠBoolean ): ĠBoolean Ġ{ Ċĉ ĉ Permission ManagerImpl . check Permissions ( activity Ġ= Ġactivity , Ġpermissions Ġ= ĠarrayOf ( Manifest . permission . CAMERA , ĠManifest . permission . READ _ EXTERNAL _ STORAGE , ĠManifest . permission . WRITE _ EXTERNAL _ STORAGE ), Ġon Permission Result Ġ= Ġ{ Ġ_ Ġ-> Ċĉĉ ĉ if Ġ( is Gallery ) Ġ{ Ċĉĉĉ ĉ select From Image Gallery () Ċĉĉ ĉ } Ġelse Ġ{ Ċĉĉĉ ĉ take Photo Pressed () Ċĉĉ ĉ } Ċĉ ĉ }, Ġpermission Request Pre Execute Explanation Ġ= Ġnull , Ġpermission Request Retry Explanation Ġ= Ġnull , Ġrequest Code Ġ= Ġ 1 2 3 ) Ċĉ ĉ return Ġfalse Ċ ĉ } ĊĊ ĉ private Ġfun Ġtake Photo Pressed () Ġ{ Ċĉ ĉ if Ġ( is Device Support Camera ( activity )) Ġ{ Ċĉĉ ĉ get Camera Intent Ġ{ Ġintent Resolved , Ġintent , Ġuri Ġ-> Ċĉĉĉ ĉ if Ġ( intent Resolved ) Ġ{ Ċĉĉĉĉ ĉ file Uri Ġ= Ġuri Ċĉĉĉĉ ĉ activity . startActivity For Result ( intent , ĠREQUEST _ CODE _ TA KE _ PH OTO ) Ċĉĉĉ ĉ } Ċĉĉ ĉ } Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ private Ġfun Ġselect From Image Gallery () Ġ{ Ċĉ ĉ activity . startActivity For Result ( get Photo Gallery Intent (), ĠREQUEST _ CODE _ IMAGE _ FROM _ G ALL ERY ) Ċ ĉ } ĊĊ ĉ private Ġfun Ġselect From Video Gallery () Ġ{ Ċĉ ĉ activity . startActivity For Result ( get Video Gallery Intent (), ĠREQUEST _ CODE _ VIDEO _ FROM _ G ALL ERY ) Ċ ĉ } Ċ }