Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 42fdb08

Browse files
author
Ferry Spiering
committed
Fix java file formatting
1 parent 1535a56 commit 42fdb08

File tree

6 files changed

+266
-265
lines changed

6 files changed

+266
-265
lines changed

packages/webview_flutter/android/src/main/java/io/flutter/plugins/webviewflutter/Constants.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
package io.flutter.plugins.webviewflutter;
66

77
public class Constants {
8-
static final String ACTION_REQUEST_CAMERA_PERMISSION_FINISHED = "action_request_camera_permission_denied";
9-
static final String ACTION_FILE_CHOOSER_FINISHED = "action_file_chooser_completed";
8+
static final String ACTION_REQUEST_CAMERA_PERMISSION_FINISHED =
9+
"action_request_camera_permission_denied";
10+
static final String ACTION_FILE_CHOOSER_FINISHED = "action_file_chooser_completed";
1011

11-
static final String EXTRA_TITLE = "extra_title";
12-
static final String EXTRA_TYPE = "extra_type";
13-
static final String EXTRA_SHOW_CAMERA_OPTION = "extra_show_camera_option";
14-
static final String EXTRA_FILE_URI = "extra_file_uri";
12+
static final String EXTRA_TITLE = "extra_title";
13+
static final String EXTRA_TYPE = "extra_type";
14+
static final String EXTRA_SHOW_CAMERA_OPTION = "extra_show_camera_option";
15+
static final String EXTRA_FILE_URI = "extra_file_uri";
1516

16-
static final String WEBVIEW_STORAGE_DIRECTORY = "storage";
17+
static final String WEBVIEW_STORAGE_DIRECTORY = "storage";
1718
}

packages/webview_flutter/android/src/main/java/io/flutter/plugins/webviewflutter/FileChooserActivity.java

Lines changed: 129 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44

55
package io.flutter.plugins.webviewflutter;
66

7+
import static io.flutter.plugins.webviewflutter.Constants.ACTION_FILE_CHOOSER_FINISHED;
8+
import static io.flutter.plugins.webviewflutter.Constants.EXTRA_FILE_URI;
9+
import static io.flutter.plugins.webviewflutter.Constants.EXTRA_SHOW_CAMERA_OPTION;
10+
import static io.flutter.plugins.webviewflutter.Constants.EXTRA_TITLE;
11+
import static io.flutter.plugins.webviewflutter.Constants.EXTRA_TYPE;
12+
import static io.flutter.plugins.webviewflutter.Constants.WEBVIEW_STORAGE_DIRECTORY;
13+
714
import android.app.Activity;
815
import android.content.Intent;
916
import android.database.Cursor;
@@ -12,10 +19,8 @@
1219
import android.provider.MediaStore;
1320
import android.provider.OpenableColumns;
1421
import android.util.Log;
15-
1622
import androidx.annotation.Nullable;
1723
import androidx.core.content.FileProvider;
18-
1924
import java.io.File;
2025
import java.io.FileOutputStream;
2126
import java.io.IOException;
@@ -24,145 +29,141 @@
2429
import java.text.SimpleDateFormat;
2530
import java.util.Date;
2631

27-
import static io.flutter.plugins.webviewflutter.Constants.ACTION_FILE_CHOOSER_FINISHED;
28-
import static io.flutter.plugins.webviewflutter.Constants.EXTRA_FILE_URI;
29-
import static io.flutter.plugins.webviewflutter.Constants.EXTRA_SHOW_CAMERA_OPTION;
30-
import static io.flutter.plugins.webviewflutter.Constants.EXTRA_TITLE;
31-
import static io.flutter.plugins.webviewflutter.Constants.EXTRA_TYPE;
32-
import static io.flutter.plugins.webviewflutter.Constants.WEBVIEW_STORAGE_DIRECTORY;
33-
3432
public class FileChooserActivity extends Activity {
3533

36-
private static final int FILE_CHOOSER_REQUEST_CODE = 12322;
37-
private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss");
38-
39-
private Uri cameraImageUri;
40-
41-
@Override
42-
protected void onCreate(@Nullable Bundle savedInstanceState) {
43-
super.onCreate(savedInstanceState);
44-
showFileChooser(getIntent().getBooleanExtra(EXTRA_SHOW_CAMERA_OPTION, false));
34+
private static final int FILE_CHOOSER_REQUEST_CODE = 12322;
35+
private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss");
36+
37+
private Uri cameraImageUri;
38+
39+
@Override
40+
protected void onCreate(@Nullable Bundle savedInstanceState) {
41+
super.onCreate(savedInstanceState);
42+
showFileChooser(getIntent().getBooleanExtra(EXTRA_SHOW_CAMERA_OPTION, false));
43+
}
44+
45+
private void showFileChooser(boolean enableCamera) {
46+
Intent galleryIntent = createGalleryIntent();
47+
Intent takePictureIntent = enableCamera ? createCameraIntent() : null;
48+
if (galleryIntent == null && takePictureIntent == null) {
49+
// cannot open anything: cancel file chooser
50+
sendBroadcast(new Intent(ACTION_FILE_CHOOSER_FINISHED));
51+
finish();
52+
} else {
53+
Intent[] intentArray =
54+
takePictureIntent != null ? new Intent[] {takePictureIntent} : new Intent[] {};
55+
56+
Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
57+
chooserIntent.putExtra(
58+
Intent.EXTRA_INTENT, galleryIntent != null ? galleryIntent : takePictureIntent);
59+
chooserIntent.putExtra(Intent.EXTRA_TITLE, getIntent().getStringExtra(EXTRA_TITLE));
60+
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
61+
62+
startActivityForResult(chooserIntent, FILE_CHOOSER_REQUEST_CODE);
4563
}
46-
47-
private void showFileChooser(boolean enableCamera) {
48-
Intent galleryIntent = createGalleryIntent();
49-
Intent takePictureIntent = enableCamera ? createCameraIntent() : null;
50-
if (galleryIntent == null && takePictureIntent == null) {
51-
// cannot open anything: cancel file chooser
52-
sendBroadcast(new Intent(ACTION_FILE_CHOOSER_FINISHED));
53-
finish();
54-
} else {
55-
Intent[] intentArray = takePictureIntent != null ? new Intent[] { takePictureIntent } : new Intent[]{};
56-
57-
Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
58-
chooserIntent.putExtra(Intent.EXTRA_INTENT, galleryIntent != null ? galleryIntent : takePictureIntent);
59-
chooserIntent.putExtra(Intent.EXTRA_TITLE, getIntent().getStringExtra(EXTRA_TITLE));
60-
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
61-
62-
startActivityForResult(chooserIntent, FILE_CHOOSER_REQUEST_CODE);
63-
}
64+
}
65+
66+
private Intent createGalleryIntent() {
67+
Intent filesIntent = new Intent(Intent.ACTION_GET_CONTENT);
68+
filesIntent.setType(getIntent().getStringExtra(EXTRA_TYPE));
69+
return (filesIntent.resolveActivity(getPackageManager()) != null) ? filesIntent : null;
70+
}
71+
72+
private Intent createCameraIntent() {
73+
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
74+
if (takePictureIntent.resolveActivity(getPackageManager()) == null) {
75+
return null;
6476
}
77+
// Create the File where the photo should go
78+
cameraImageUri = getTempImageUri();
79+
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, cameraImageUri);
6580

66-
private Intent createGalleryIntent() {
67-
Intent filesIntent = new Intent(Intent.ACTION_GET_CONTENT);
68-
filesIntent.setType(getIntent().getStringExtra(EXTRA_TYPE));
69-
return (filesIntent.resolveActivity(getPackageManager()) != null) ? filesIntent : null;
70-
}
71-
72-
private Intent createCameraIntent() {
73-
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
74-
if (takePictureIntent.resolveActivity(getPackageManager()) == null) {
75-
return null;
76-
}
77-
// Create the File where the photo should go
78-
cameraImageUri = getTempImageUri();
79-
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, cameraImageUri);
81+
return takePictureIntent;
82+
}
8083

81-
return takePictureIntent;
84+
private File getStorageDirectory() {
85+
File imageDirectory = new File(getCacheDir(), WEBVIEW_STORAGE_DIRECTORY);
86+
if (!imageDirectory.exists() && !imageDirectory.mkdir()) {
87+
Log.e("WEBVIEW", "Unable to create storage directory");
8288
}
83-
84-
private File getStorageDirectory() {
85-
File imageDirectory = new File(getCacheDir(), WEBVIEW_STORAGE_DIRECTORY);
86-
if (!imageDirectory.exists() && !imageDirectory.mkdir()) {
87-
Log.e("WEBVIEW", "Unable to create storage directory");
89+
return imageDirectory;
90+
}
91+
92+
private Uri getTempImageUri() {
93+
String imageFileName = "IMG-" + simpleDateFormat.format(new Date()) + ".jpg";
94+
File imageFile = new File(getStorageDirectory(), imageFileName);
95+
return FileProvider.getUriForFile(
96+
this, getApplicationContext().getPackageName() + ".generic.provider", imageFile);
97+
}
98+
99+
private String getFileNameFromUri(Uri uri) {
100+
Cursor returnCursor = getContentResolver().query(uri, null, null, null, null);
101+
assert returnCursor != null;
102+
int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
103+
returnCursor.moveToFirst();
104+
String name = returnCursor.getString(nameIndex);
105+
returnCursor.close();
106+
return name;
107+
}
108+
109+
private Uri copyToLocalUri(Uri uri) {
110+
InputStream in = null;
111+
OutputStream out = null;
112+
try {
113+
File destination = new File(getStorageDirectory(), getFileNameFromUri(uri));
114+
in = getContentResolver().openInputStream(uri);
115+
out = new FileOutputStream(destination);
116+
117+
int cnt = 0;
118+
byte[] buffer = new byte[1024];
119+
int len;
120+
while ((len = in.read(buffer)) != -1) {
121+
out.write(buffer, 0, len);
122+
cnt += len;
123+
}
124+
125+
return FileProvider.getUriForFile(
126+
this, getApplicationContext().getPackageName() + ".generic.provider", destination);
127+
} catch (Exception e) {
128+
Log.e("WEBVIEW", "Unable to copy selected image", e);
129+
} finally {
130+
if (in != null) {
131+
try {
132+
in.close();
133+
} catch (IOException e) {
134+
e.printStackTrace();
88135
}
89-
return imageDirectory;
90-
}
91-
92-
private Uri getTempImageUri() {
93-
String imageFileName = "IMG-" + simpleDateFormat.format(new Date()) + ".jpg";
94-
File imageFile = new File(getStorageDirectory(), imageFileName);
95-
return FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".generic.provider", imageFile);
96-
}
97-
98-
private String getFileNameFromUri(Uri uri) {
99-
Cursor returnCursor = getContentResolver().query(uri, null, null, null, null);
100-
assert returnCursor != null;
101-
int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
102-
returnCursor.moveToFirst();
103-
String name = returnCursor.getString(nameIndex);
104-
returnCursor.close();
105-
return name;
106-
}
107-
108-
private Uri copyToLocalUri(Uri uri) {
109-
InputStream in = null;
110-
OutputStream out = null;
136+
}
137+
if (out != null) {
111138
try {
112-
File destination = new File(getStorageDirectory(), getFileNameFromUri(uri));
113-
in = getContentResolver().openInputStream(uri);
114-
out = new FileOutputStream(destination);
115-
116-
int cnt = 0;
117-
byte[] buffer = new byte[1024];
118-
int len;
119-
while ((len = in.read(buffer)) != -1) {
120-
out.write(buffer, 0, len);
121-
cnt += len;
122-
}
123-
124-
return FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".generic.provider", destination);
125-
} catch(Exception e){
126-
Log.e("WEBVIEW", "Unable to copy selected image", e);
127-
} finally {
128-
if (in != null) {
129-
try {
130-
in.close();
131-
} catch (IOException e) {
132-
e.printStackTrace();
133-
}
134-
}
135-
if (out != null) {
136-
try {
137-
out.close();
138-
} catch (IOException e) {
139-
e.printStackTrace();
140-
}
141-
}
139+
out.close();
140+
} catch (IOException e) {
141+
e.printStackTrace();
142142
}
143-
144-
return null;
143+
}
145144
}
146145

147-
@Override
148-
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
149-
if (requestCode == FILE_CHOOSER_REQUEST_CODE) {
150-
Intent intent = new Intent(ACTION_FILE_CHOOSER_FINISHED);
151-
if (resultCode == Activity.RESULT_OK) {
152-
if (data != null && data.getDataString() != null) {
153-
// result from file browser
154-
final Uri uri = copyToLocalUri(data.getData());
155-
intent.putExtra(EXTRA_FILE_URI, uri.toString());
156-
} else {
157-
// result from camera
158-
intent.putExtra(EXTRA_FILE_URI, cameraImageUri.toString());
159-
}
160-
}
161-
sendBroadcast(intent);
162-
finish();
146+
return null;
147+
}
148+
149+
@Override
150+
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
151+
if (requestCode == FILE_CHOOSER_REQUEST_CODE) {
152+
Intent intent = new Intent(ACTION_FILE_CHOOSER_FINISHED);
153+
if (resultCode == Activity.RESULT_OK) {
154+
if (data != null && data.getDataString() != null) {
155+
// result from file browser
156+
final Uri uri = copyToLocalUri(data.getData());
157+
intent.putExtra(EXTRA_FILE_URI, uri.toString());
163158
} else {
164-
super.onActivityResult(requestCode, resultCode, data);
159+
// result from camera
160+
intent.putExtra(EXTRA_FILE_URI, cameraImageUri.toString());
165161
}
162+
}
163+
sendBroadcast(intent);
164+
finish();
165+
} else {
166+
super.onActivityResult(requestCode, resultCode, data);
166167
}
167-
168+
}
168169
}

0 commit comments

Comments
 (0)