This repository has been archived by the owner on Feb 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathNoNonsenseFilePicker.java
202 lines (175 loc) · 8.12 KB
/
NoNonsenseFilePicker.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package com.nononsenseapps.filepicker.sample;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import com.nononsenseapps.filepicker.AbstractFilePickerFragment;
import com.nononsenseapps.filepicker.FilePickerActivity;
import com.nononsenseapps.filepicker.Utils;
import com.nononsenseapps.filepicker.sample.databinding.ActivityNoNonsenseFilePickerBinding;
import com.nononsenseapps.filepicker.sample.dropbox.DropboxFilePickerActivity;
import com.nononsenseapps.filepicker.sample.dropbox.DropboxFilePickerActivity2;
import com.nononsenseapps.filepicker.sample.fastscroller.FastScrollerFilePickerActivity;
import com.nononsenseapps.filepicker.sample.fastscroller.FastScrollerFilePickerActivity2;
import com.nononsenseapps.filepicker.sample.ftp.FtpPickerActivity;
import com.nononsenseapps.filepicker.sample.ftp.FtpPickerActivity2;
import com.nononsenseapps.filepicker.sample.multimedia.MultimediaPickerActivity;
import com.nononsenseapps.filepicker.sample.multimedia.MultimediaPickerActivity2;
import com.nononsenseapps.filepicker.sample.root.SUPickerActivity;
import com.nononsenseapps.filepicker.sample.root.SUPickerActivity2;
import java.util.List;
public class NoNonsenseFilePicker extends Activity {
// How to handle multiple return data
public static boolean useClipData = true;
static final int CODE_SD = 0;
static final int CODE_DB = 1;
static final int CODE_FTP = 2;
ActivityNoNonsenseFilePickerBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_no_nonsense_file_picker);
binding.buttonSd
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
if (binding.checkLightTheme.isChecked()) {
startActivity(CODE_SD, FilePickerActivity2.class);
} else {
startActivity(CODE_SD, FilePickerActivity.class);
}
}
});
binding.buttonImage
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
if (binding.checkLightTheme.isChecked()) {
startActivity(CODE_SD, MultimediaPickerActivity2.class);
} else {
startActivity(CODE_SD, MultimediaPickerActivity.class);
}
}
});
binding.buttonFtp
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
if (binding.checkLightTheme.isChecked()) {
startActivity(CODE_FTP, FtpPickerActivity2.class);
} else {
startActivity(CODE_FTP, FtpPickerActivity.class);
}
}
});
binding.buttonDropbox
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
if (binding.checkLightTheme.isChecked()) {
startActivity(CODE_DB, DropboxFilePickerActivity2.class);
} else {
startActivity(CODE_DB, DropboxFilePickerActivity.class);
}
}
});
binding.buttonRoot
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (binding.checkLightTheme.isChecked()) {
startActivity(CODE_SD, SUPickerActivity.class);
} else {
startActivity(CODE_SD, SUPickerActivity2.class);
}
}
});
binding.buttonFastscroll
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (binding.checkLightTheme.isChecked()) {
startActivity(CODE_SD, FastScrollerFilePickerActivity.class);
} else {
startActivity(CODE_SD, FastScrollerFilePickerActivity2.class);
}
}
});
}
protected void startActivity(final int code, final Class<?> klass) {
final Intent i = new Intent(this, klass);
i.setAction(Intent.ACTION_GET_CONTENT);
i.putExtra(SUPickerActivity.EXTRA_ALLOW_MULTIPLE, binding.checkAllowMultiple.isChecked());
i.putExtra(FilePickerActivity.EXTRA_SINGLE_CLICK, binding.checkSingleClick.isChecked());
i.putExtra(SUPickerActivity.EXTRA_ALLOW_CREATE_DIR, binding.checkAllowCreateDir.isChecked());
i.putExtra(FilePickerActivity.EXTRA_ALLOW_EXISTING_FILE, binding.checkAllowExistingFile.isChecked());
// What mode is selected
final int mode;
switch (binding.radioGroup.getCheckedRadioButtonId()) {
case R.id.radioDir:
mode = AbstractFilePickerFragment.MODE_DIR;
break;
case R.id.radioFilesAndDirs:
mode = AbstractFilePickerFragment.MODE_FILE_AND_DIR;
break;
case R.id.radioNewFile:
mode = AbstractFilePickerFragment.MODE_NEW_FILE;
break;
case R.id.radioFile:
default:
mode = AbstractFilePickerFragment.MODE_FILE;
break;
}
i.putExtra(FilePickerActivity.EXTRA_MODE, mode);
// This line is solely so that test classes can override intents given through UI
i.putExtras(getIntent());
startActivityForResult(i, code);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.no_nonsense_file_picker, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
return id == R.id.action_settings || super.onOptionsItemSelected(item);
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Always check the resultCode!
// Checking for the requestCodes is a bit redundant but good style
if (resultCode == Activity.RESULT_OK &&
(CODE_SD == requestCode || CODE_DB == requestCode || CODE_FTP == requestCode)) {
// Use the provided utility method to parse the result
List<Uri> files = Utils.getSelectedFilesFromResult(data);
// Do something with your list of files here
StringBuilder sb = new StringBuilder();
for (Uri uri : files) {
if (sb.length() > 0) {
sb.append("\n");
}
sb.append(CODE_SD == requestCode ?
Utils.getFileForUri(uri).toString() :
uri.toString());
}
binding.text.setText(sb.toString());
}
}
}