11package app .gwo .wechat .docuiproxy ;
22
3+ import android .Manifest ;
4+ import android .app .AlertDialog ;
35import android .content .ActivityNotFoundException ;
46import android .content .ComponentName ;
7+ import android .content .Context ;
58import android .content .Intent ;
69import android .net .Uri ;
10+ import android .os .Build ;
711import android .os .Bundle ;
812import android .provider .MediaStore ;
913import android .util .Log ;
14+ import android .view .ContextThemeWrapper ;
15+ import android .view .LayoutInflater ;
16+ import android .view .View ;
1017
1118import androidx .annotation .NonNull ;
1219import androidx .annotation .Nullable ;
1320import app .gwo .wechat .docuiproxy .util .DumpUtils ;
1421import app .gwo .wechat .docuiproxy .util .Settings ;
1522
23+ import static android .content .pm .PackageManager .PERMISSION_GRANTED ;
1624import static app .gwo .wechat .docuiproxy .BuildConfig .DEBUG ;
1725import static java .util .Objects .requireNonNull ;
1826
@@ -27,11 +35,15 @@ public final class ProxyCameraActivity extends BaseActivity {
2735
2836 public static final int REQUEST_CODE_OPEN = 1 ;
2937 public static final int REQUEST_CODE_CAPTURE = 2 ;
38+ public static final int REQUEST_CODE_REQUEST_CAMERA_PERMISSION = 3 ;
3039
3140 private boolean shouldBeHandled = false ;
3241
3342 private Uri mExpectedOutput = null ;
3443
44+ @ Nullable
45+ private ComponentName mExpectedCameraComponent = null ;
46+
3547 @ Override
3648 protected void onCreate (@ Nullable Bundle savedInstanceState ) {
3749 super .onCreate (savedInstanceState );
@@ -79,10 +91,22 @@ private void getExtrasFromCaptureIntent(@NonNull Intent intent) {
7991 }
8092
8193 private void processIntentForWeChat (@ NonNull Intent intent ) {
82- // TODO Choose Documents UI or camera app
83- Intent openIntent = new Intent (Intent .ACTION_GET_CONTENT );
84- openIntent .setType ("image/*" );
85- startActivityForResult (openIntent , REQUEST_CODE_OPEN );
94+ final Context themedContext = new ContextThemeWrapper (
95+ this , android .R .style .Theme_Material_Light_Dialog );
96+ final View view = LayoutInflater .from (themedContext )
97+ .inflate (R .layout .dialog_doc_or_cam_chooser_content , null );
98+ view .findViewById (R .id .action_camera ).setOnClickListener (v -> {
99+ processIntentForOthers (intent );
100+ });
101+ view .findViewById (R .id .action_documents ).setOnClickListener (v -> {
102+ Intent openIntent = new Intent (Intent .ACTION_GET_CONTENT );
103+ openIntent .setType ("image/*" );
104+ startActivityForResult (openIntent , REQUEST_CODE_OPEN );
105+ });
106+ new AlertDialog .Builder (themedContext )
107+ .setView (view )
108+ .setNegativeButton (android .R .string .cancel , null )
109+ .show ();
86110 }
87111
88112 private void processIntentForOthers (@ NonNull Intent intent ) {
@@ -149,6 +173,30 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
149173 }
150174 }
151175
176+ @ Override
177+ public void onRequestPermissionsResult (int requestCode ,
178+ @ NonNull String [] permissions ,
179+ @ NonNull int [] grantResults ) {
180+ switch (requestCode ) {
181+ case REQUEST_CODE_REQUEST_CAMERA_PERMISSION :
182+ if (!Manifest .permission .CAMERA .equals (permissions [0 ]) ||
183+ PERMISSION_GRANTED != grantResults [0 ]) {
184+ Log .e (TAG , "No permission." );
185+ finish ();
186+ return ;
187+ }
188+ if (mExpectedCameraComponent != null ) {
189+ onStartCameraApp (mExpectedCameraComponent );
190+ } else {
191+ finish ();
192+ }
193+ break ;
194+ default :
195+ Log .e (TAG , "Unsupported result." );
196+ finish ();
197+ }
198+ }
199+
152200 void onCopyResult (boolean success ) {
153201 if (success ) {
154202 setResult (RESULT_OK , new Intent ());
@@ -160,6 +208,16 @@ void onCopyResult(boolean success) {
160208
161209 void onStartCameraApp (@ NonNull ComponentName target ) {
162210 requireNonNull (target );
211+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
212+ if (checkSelfPermission (Manifest .permission .CAMERA ) != PERMISSION_GRANTED ) {
213+ mExpectedCameraComponent = target ;
214+ requestPermissions (
215+ new String [] { Manifest .permission .CAMERA },
216+ REQUEST_CODE_REQUEST_CAMERA_PERMISSION
217+ );
218+ return ;
219+ }
220+ }
163221 Intent cameraIntent = new Intent (MediaStore .ACTION_IMAGE_CAPTURE );
164222 cameraIntent .setComponent (target );
165223 cameraIntent .addFlags (Intent .FLAG_ACTIVITY_MULTIPLE_TASK );
0 commit comments