Skip to content

Commit 693dcd1

Browse files
committed
ContentUtils + IOUtils
1 parent c35d7e3 commit 693dcd1

File tree

2 files changed

+201
-0
lines changed

2 files changed

+201
-0
lines changed
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
package org.andresoviedo.app.util.android;
2+
3+
import android.annotation.TargetApi;
4+
import android.app.Activity;
5+
import android.app.AlertDialog;
6+
import android.content.DialogInterface;
7+
import android.content.Intent;
8+
import android.content.pm.PackageManager;
9+
import android.net.Uri;
10+
import android.os.Build;
11+
import android.util.Log;
12+
13+
import java.io.File;
14+
import java.io.IOException;
15+
import java.io.InputStream;
16+
import java.net.URI;
17+
import java.util.HashMap;
18+
import java.util.Map;
19+
20+
public class ContentUtils {
21+
22+
/**
23+
* Documents opened by the user. This list helps finding the relative filenames found in the model
24+
*/
25+
private static Map<String,Uri> documentsProvided = new HashMap<>();
26+
27+
private static ThreadLocal<Activity> currentActivity = new ThreadLocal<>();
28+
29+
private static File currentDir = null;
30+
31+
public static void printTouchCapabilities(PackageManager packageManager) {
32+
if (packageManager.hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN_MULTITOUCH)) {
33+
Log.i("utils", "System supports multitouch (2 fingers)");
34+
}
35+
if (packageManager.hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN_MULTITOUCH_DISTINCT)) {
36+
Log.i("utils", "System supports advanced multitouch (multiple fingers)");
37+
}
38+
}
39+
40+
public static void setThreadActivity(Activity currentActivity) {
41+
Log.i("ContentUtils","Current activity thread: "+Thread.currentThread().getName());
42+
ContentUtils.currentActivity.set(currentActivity);
43+
}
44+
45+
private static Activity getCurrentActivity(){
46+
return ContentUtils.currentActivity.get();
47+
}
48+
49+
public static void setCurrentDir(File file){
50+
ContentUtils.currentDir = file;
51+
}
52+
53+
public static void clearDocumentsProvided(){
54+
// clear documents provided by user (kitkat only)
55+
documentsProvided.clear();
56+
}
57+
58+
public static void provideAssets(Activity activity) {
59+
documentsProvided.clear();
60+
try {
61+
for (String document : activity.getAssets().list("models")) {
62+
documentsProvided.put(document, Uri.parse("assets://assets/models/" + document));
63+
}
64+
}catch(IOException ex){
65+
Log.e("ContentUtils","Error listing assets from models folder", ex);
66+
}
67+
}
68+
69+
public static void addUri(String name, Uri uri){
70+
documentsProvided.put(name, uri);
71+
Log.i("ContentUtils","Added ("+name+") "+uri);
72+
}
73+
74+
public static Uri getUri(String name){
75+
return documentsProvided.get(name);
76+
}
77+
78+
/**
79+
* Find the relative file that should be already selected by the user
80+
* @param path relative file
81+
* @return InputStream of the file
82+
* @throws IOException if there is an error opening stream
83+
*/
84+
public static InputStream getInputStream(String path) throws IOException {
85+
Uri uri = getUri(path);
86+
if (uri == null && currentDir != null){
87+
uri = Uri.parse("file://"+new File(currentDir, path).getAbsolutePath());
88+
}
89+
if (uri != null){
90+
return getInputStream(uri);
91+
}
92+
return null;
93+
}
94+
95+
public static InputStream getInputStream(URI uri) throws IOException {
96+
return getInputStream(Uri.parse(uri.toString()));
97+
}
98+
99+
public static InputStream getInputStream(Uri uri) throws IOException {
100+
Log.i("ContentUtils","Opening stream "+uri.getPath());
101+
if (uri.getScheme().equals("assets")){
102+
Log.i("ContentUtils","Opening asset: "+uri.getPath());
103+
return getCurrentActivity().getAssets().open(uri.getPath().substring(1));
104+
}
105+
return getCurrentActivity().getContentResolver().openInputStream(uri);
106+
}
107+
108+
109+
110+
public static Intent createGetContentIntent(String mimeType) {
111+
// check here to KITKAT or new version
112+
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
113+
if (isKitKat){
114+
return createGetMultipleContentIntent(mimeType);
115+
}
116+
return createGetSingleContentIntent(mimeType);
117+
}
118+
119+
/**
120+
* Get the Intent for selecting content to be used in an Intent Chooser.
121+
*
122+
* @return The intent for opening a file with Intent.createChooser()
123+
*
124+
* @author paulburke
125+
*/
126+
@TargetApi(Build.VERSION_CODES.KITKAT)
127+
private static Intent createGetMultipleContentIntent(String mimeType) {
128+
// Implicitly allow the user to select a particular kind of data
129+
final Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
130+
// The MIME data type filter
131+
intent.setType(mimeType);
132+
// EXTRA_ALLOW_MULTIPLE: added in API level 18
133+
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
134+
// Only return URIs that can be opened with ContentResolver
135+
intent.addCategory(Intent.CATEGORY_OPENABLE);
136+
return intent;
137+
}
138+
139+
/**
140+
* Get the Intent for selecting content to be used in an Intent Chooser.
141+
*
142+
* @return The intent for opening a file with Intent.createChooser()
143+
*
144+
* @author paulburke
145+
*/
146+
@TargetApi(Build.VERSION_CODES.KITKAT)
147+
private static Intent createGetSingleContentIntent(String mimeType) {
148+
// Implicitly allow the user to select a particular kind of data
149+
final Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
150+
// The MIME data type filter
151+
intent.setType(mimeType);
152+
// Only return URIs that can be opened with ContentResolver
153+
intent.addCategory(Intent.CATEGORY_OPENABLE);
154+
return intent;
155+
}
156+
157+
public static void showDialog(Activity activity, String title, CharSequence message, String positiveButtonLabel,
158+
String negativeButtonLabel, DialogInterface.OnClickListener listener) {
159+
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
160+
builder.setTitle(title);
161+
builder.setMessage(message);
162+
builder.setPositiveButton(positiveButtonLabel, listener);
163+
builder.setNegativeButton(negativeButtonLabel, listener);
164+
builder.create().show();
165+
}
166+
167+
public static void showListDialog(Activity activity, String title, String[] options, DialogInterface
168+
.OnClickListener listener){
169+
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
170+
builder.setTitle(title).setItems(options, listener);
171+
builder.create().show();
172+
}
173+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.andresoviedo.app.util.io;
2+
3+
import java.io.ByteArrayOutputStream;
4+
import java.io.File;
5+
import java.io.FileInputStream;
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
9+
public final class IOUtils {
10+
11+
public static byte[] read(File file) throws IOException {
12+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
13+
FileInputStream fis = new FileInputStream(file);
14+
byte[] data = read(fis);
15+
fis.close();
16+
return data;
17+
}
18+
19+
public static byte[] read(InputStream is) throws IOException {
20+
byte[] isData = new byte[512];
21+
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
22+
int nRead;
23+
while ((nRead = is.read(isData, 0, isData.length)) != -1) {
24+
buffer.write(isData, 0, nRead);
25+
}
26+
return buffer.toByteArray();
27+
}
28+
}

0 commit comments

Comments
 (0)