/
FilePickerBuilder.java
125 lines (111 loc) · 3.48 KB
/
FilePickerBuilder.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
package com.github.developerpaul123.filepickerlibrary;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.ColorRes;
import com.github.developerpaul123.filepickerlibrary.enums.MimeType;
import com.github.developerpaul123.filepickerlibrary.enums.Request;
import com.github.developerpaul123.filepickerlibrary.enums.Scope;
/**
* Created by Paul on 11/23/2015.
*/
public class FilePickerBuilder {
private final Context mContext;
private boolean useMaterial;
private Scope mScope = Scope.ALL;
private Request requestCode = Request.FILE;
private int color = android.R.color.holo_blue_bright;
private MimeType mimeType;
/**
* Builder class to build a filepicker activity.
*
* @param context the calling activity.
*/
public FilePickerBuilder(Context context) {
mContext = context;
}
@Deprecated
public FilePickerBuilder withScopeType(Scope type) {
mScope = type;
return this;
}
/**
* Set the scopetype of the file picker.
*
* @param type scope type. Can be DIRECTORIES or ALL.
* @return the current builder instance.
*/
public FilePickerBuilder withScope(Scope type) {
mScope = type;
return this;
}
/**
* Set the request code of this. You can request a path to a file or
* a directory.
*
* @param request
* @return
*/
public FilePickerBuilder withRequest(Request request) {
requestCode = request;
return this;
}
/**
* Set the header color with a resource integer.
*
* @param color the color resource.
* @return current instance of tbe builder.
*/
public FilePickerBuilder withColor(@ColorRes int color) {
this.color = color;
return this;
}
/**
* Set the file mime type. The will require the returned file type to match the
* mime type.
*
* @param type the mime type.
* @return current instance of the builder.
*/
public FilePickerBuilder withMimeType(MimeType type) {
mimeType = type;
return this;
}
/**
* Set if you want to use the new material designed file picker.
*
* @param use true to use the material version. False otherwise, default is false.
* @return current builder instance.
*/
public FilePickerBuilder useMaterialActivity(boolean use) {
useMaterial = use;
return this;
}
@Deprecated
public void launch() {
Intent intent = build();
((Activity) mContext).startActivityForResult(intent, requestCode.ordinal());
}
/**
* Build the current intent.
*
* @return a filepicker intent.
*/
public Intent build() {
Intent filePicker = new Intent(mContext, useMaterial ? FilePicker.class : FilePickerActivity.class);
filePicker.putExtra(FilePicker.SCOPE, mScope);
filePicker.putExtra(FilePicker.REQUEST, requestCode);
filePicker.putExtra(FilePicker.INTENT_EXTRA_COLOR_ID, color);
filePicker.putExtra(FilePicker.MIME_TYPE, mimeType);
return filePicker;
}
/**
* Builds and starts the intent with startActivityForResult() uses the request code you said as the request code for the activity result.
*
* @param requestCode
*/
public void launch(int requestCode) {
Intent intent = build();
((Activity) mContext).startActivityForResult(intent, requestCode);
}
}