-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathFileActivity.java
More file actions
229 lines (181 loc) · 6.15 KB
/
FileActivity.java
File metadata and controls
229 lines (181 loc) · 6.15 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package com.bd.gitlab;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringEscapeUtils;
import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.MimeTypeMap;
import android.webkit.WebView;
import com.bd.gitlab.tools.Repository;
import com.bd.gitlab.tools.RetrofitHelper;
import de.keyboardsurfer.android.widget.crouton.Crouton;
import de.keyboardsurfer.android.widget.crouton.Style;
public class FileActivity extends ActionBarActivity {
@InjectView(R.id.toolbar) Toolbar toolbar;
@InjectView(R.id.file_blob) WebView fileBlobView;
private MenuItem openFile;
private MenuItem saveFile;
private byte[] fileBlob;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_file);
ButterKnife.inject(this);
setSupportActionBar(toolbar);
if(Repository.selectedFile != null) {
setupUI();
Repository.getService().getBlob(Repository.selectedProject.getId(), Repository.newestCommit.getId(), getIntent().getExtras().getString("path") + Repository.selectedFile.getName(), blobCallback);
}
}
@Override
public void onDestroy() {
super.onDestroy();
Crouton.cancelAllCroutons();
}
@SuppressLint("SetJavaScriptEnabled")
private void setupUI() {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(Repository.selectedFile.getName());
getSupportActionBar().setIcon(getResources().getDrawable(R.drawable.ic_actionbar));
fileBlobView.getSettings().setJavaScriptEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.file, menu);
openFile = menu.getItem(0);
saveFile = menu.getItem(1);
return true;
}
private void enableMenu() {
if(openFile != null) {
openFile.setEnabled(true);
openFile.setIcon(R.drawable.ic_action_open);
}
if(saveFile != null) {
saveFile.setEnabled(true);
saveFile.setIcon(R.drawable.ic_action_save);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case android.R.id.home:
finish();
return true;
case R.id.action_open:
openFile();
return true;
case R.id.action_save:
saveBlob();
return true;
}
return super.onOptionsItemSelected(item);
}
private Callback<Response> blobCallback = new Callback<Response>() {
@Override
public void success(Response response, Response resp) {
String content = getResources().getString(R.string.file_load_error);
try {
fileBlob = IOUtils.toByteArray(response.getBody().in());
content = new String(fileBlob, "UTF-8");
}
catch(UnsupportedEncodingException e) {
e.printStackTrace();
}
catch(IOException e) {
e.printStackTrace();
}
String temp = "<!DOCTYPE html><html><head><link href=\"github.css\" rel=\"stylesheet\" /></head><body><pre><code>" + StringEscapeUtils.escapeHtml(content) + "</code></pre><script src=\"highlight.pack.js\"></script><script>hljs.initHighlightingOnLoad();</script></body></html>";
fileBlobView.loadDataWithBaseURL("file:///android_asset/", temp, "text/html", "utf8", null);
enableMenu();
}
@Override
public void failure(RetrofitError e) {
RetrofitHelper.printDebugInfo(FileActivity.this, e);
Crouton.makeText(FileActivity.this, R.string.connection_error, Style.ALERT).show();
}
};
private File saveBlob() {
String state = Environment.getExternalStorageState();
if(Environment.MEDIA_MOUNTED.equals(state) && fileBlob != null) {
File downloadFolder = new File(Environment.getExternalStorageDirectory(), "Download");
if(!downloadFolder.exists())
downloadFolder.mkdir();
File newFile = new File(downloadFolder, Repository.selectedFile.getName());
try {
FileOutputStream f = new FileOutputStream(newFile);
f.write(fileBlob);
f.close();
Crouton.makeText(this, R.string.file_saved, Style.CONFIRM).show();
return newFile;
}
catch(FileNotFoundException e) {
Crouton.makeText(this, R.string.save_error, Style.ALERT).show();
}
catch(IOException e) {
Crouton.makeText(this, R.string.save_error, Style.ALERT).show();
}
}
else
Crouton.makeText(this, R.string.save_error, Style.ALERT).show();
return null;
}
private void openFile() {
File file = saveBlob();
if(file == null) {
Crouton.makeText(this, R.string.open_error, Style.ALERT).show();
return;
}
MimeTypeMap myMime = MimeTypeMap.getSingleton();
Intent newIntent = new Intent(Intent.ACTION_VIEW);
String fileExt = fileExt(file.toString());
if (fileExt == null) {
Crouton.makeText(this, R.string.open_error, Style.ALERT).show();
return;
}
String mimeType = myMime.getMimeTypeFromExtension(fileExt.substring(1));
newIntent.setDataAndType(Uri.fromFile(file), mimeType);
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
startActivity(newIntent);
}
catch(android.content.ActivityNotFoundException e) {
Crouton.makeText(this, R.string.open_error, Style.ALERT).show();
}
}
private String fileExt(String url) {
if(url.contains("?")) {
url = url.substring(0, url.indexOf("?"));
}
if(!url.contains(".")) {
return null;
}
else {
String ext = url.substring(url.lastIndexOf("."));
if(ext.contains("%")) {
ext = ext.substring(0, ext.indexOf("%"));
}
if(ext.contains("/")) {
ext = ext.substring(0, ext.indexOf("/"));
}
return ext.toLowerCase();
}
}
}