Skip to content

Commit 3ee87ab

Browse files
committed
Add camera/documents chooser for proxy apps
Signed-off-by: Fung Gwo <fython@163.com>
1 parent 5ba908b commit 3ee87ab

7 files changed

Lines changed: 136 additions & 5 deletions

File tree

app/src/main/java/app/gwo/wechat/docuiproxy/BaseActivity.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.net.Uri;
55
import android.os.Build;
66
import android.util.Log;
7+
import android.view.MenuItem;
78

89
import java.lang.reflect.Field;
910

@@ -53,4 +54,13 @@ public String getReferrerPackage() {
5354
return BaseActivity.getReferrerPackage(this);
5455
}
5556

57+
@Override
58+
public boolean onOptionsItemSelected(MenuItem item) {
59+
if (android.R.id.home == item.getItemId()) {
60+
onBackPressed();
61+
return true;
62+
}
63+
return super.onOptionsItemSelected(item);
64+
}
65+
5666
}

app/src/main/java/app/gwo/wechat/docuiproxy/CameraChooserDialogFragment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void onReceive(@NonNull Context context, @Nullable Intent intent) {
6464

6565
@NonNull
6666
public Context getThemedContext() {
67-
return new ContextThemeWrapper(getActivity(), android.R.style.Theme_Material_Dialog);
67+
return new ContextThemeWrapper(getActivity(), android.R.style.Theme_Material_Light_Dialog);
6868
}
6969

7070
@Override

app/src/main/java/app/gwo/wechat/docuiproxy/ProxyCameraActivity.java

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
package app.gwo.wechat.docuiproxy;
22

3+
import android.Manifest;
4+
import android.app.AlertDialog;
35
import android.content.ActivityNotFoundException;
46
import android.content.ComponentName;
7+
import android.content.Context;
58
import android.content.Intent;
69
import android.net.Uri;
10+
import android.os.Build;
711
import android.os.Bundle;
812
import android.provider.MediaStore;
913
import android.util.Log;
14+
import android.view.ContextThemeWrapper;
15+
import android.view.LayoutInflater;
16+
import android.view.View;
1017

1118
import androidx.annotation.NonNull;
1219
import androidx.annotation.Nullable;
1320
import app.gwo.wechat.docuiproxy.util.DumpUtils;
1421
import app.gwo.wechat.docuiproxy.util.Settings;
1522

23+
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
1624
import static app.gwo.wechat.docuiproxy.BuildConfig.DEBUG;
1725
import 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);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="64dp"
3+
android:height="64dp"
4+
android:viewportWidth="24.0"
5+
android:viewportHeight="24.0">
6+
<path
7+
android:fillColor="?android:textColorPrimary"
8+
android:pathData="M20,6h-8l-2,-2L4,4c-1.1,0 -1.99,0.9 -1.99,2L2,18c0,1.1 0.9,2 2,2h16c1.1,0 2,-0.9 2,-2L22,8c0,-1.1 -0.9,-2 -2,-2zM20,18L4,18L4,8h16v10z"/>
9+
</vector>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="64dp"
3+
android:height="64dp"
4+
android:viewportWidth="24.0"
5+
android:viewportHeight="24.0">
6+
<path
7+
android:fillColor="?android:textColorPrimary"
8+
android:pathData="M12,12m-3.2,0a3.2,3.2 0,1 1,6.4 0a3.2,3.2 0,1 1,-6.4 0"/>
9+
<path
10+
android:fillColor="?android:textColorPrimary"
11+
android:pathData="M9,2L7.17,4L4,4c-1.1,0 -2,0.9 -2,2v12c0,1.1 0.9,2 2,2h16c1.1,0 2,-0.9 2,-2L22,6c0,-1.1 -0.9,-2 -2,-2h-3.17L15,2L9,2zM12,17c-2.76,0 -5,-2.24 -5,-5s2.24,-5 5,-5 5,2.24 5,5 -2.24,5 -5,5z"/>
12+
</vector>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:orientation="horizontal"
4+
android:layout_width="match_parent"
5+
android:layout_height="wrap_content">
6+
7+
<TextView
8+
android:id="@+id/action_camera"
9+
android:layout_width="0dp"
10+
android:layout_weight="1"
11+
android:layout_height="wrap_content"
12+
android:gravity="center"
13+
android:drawableTop="@drawable/ic_photo_camera_64dp"
14+
android:drawablePadding="8dp"
15+
android:textSize="18sp"
16+
android:textColor="?android:textColorPrimary"
17+
android:clickable="true"
18+
android:focusable="true"
19+
android:background="?android:selectableItemBackgroundBorderless"
20+
android:padding="16dp"
21+
android:text="@string/action_camera"/>
22+
23+
<TextView
24+
android:id="@+id/action_documents"
25+
android:layout_width="0dp"
26+
android:layout_weight="1"
27+
android:layout_height="wrap_content"
28+
android:gravity="center"
29+
android:drawableTop="@drawable/ic_folder_open_64dp"
30+
android:drawablePadding="8dp"
31+
android:textSize="18sp"
32+
android:textColor="?android:textColorPrimary"
33+
android:clickable="true"
34+
android:focusable="true"
35+
android:background="?android:selectableItemBackgroundBorderless"
36+
android:padding="16dp"
37+
android:text="@string/action_documents"/>
38+
39+
</LinearLayout>

app/src/main/res/values/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@
1919
<string name="action_done">Done</string>
2020
<string name="packages_selector_search_hint">Search title/package name…</string>
2121

22+
<string name="action_camera">Camera</string>
23+
<string name="action_documents">Documents</string>
24+
2225
</resources>

0 commit comments

Comments
 (0)