-
Notifications
You must be signed in to change notification settings - Fork 4
/
Main.java
477 lines (398 loc) · 14.8 KB
/
Main.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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
package com.corner23.android.usb_otg_manager;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.hardware.usb.UsbManager;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Spinner;
import android.widget.TextView;
public class Main extends Activity {
public final static String ACTION_SE_USB_DEVICE_DETACHED = "com.sonyericsson.hardware.action.USB_OTG_DEVICE_DISCONNECTED";
public final static String ACTION_SE_USB_DEVICE_ATTACHED = "com.sonyericsson.hardware.action.USB_OTG_DEVICE_CONNECTED";
public final static String MOUNT_PATH = "/mnt/sdcard/usbstorage";
public final static String PREFS_NAME = "otg_mgr_settings";
public final static String PREFS_CLOSE_ON_MOUNT = "prefs_close_on_mount";
public final static String PREFS_READ_ONLY = "prefs_read_only";
private final static String TAG = "USB_OTG_MANAGER";
private String storage_km;
private final static String FN_STORAGE_DRIVER_XPERIA = "usb_storage_xperia.ko";
private final static String FN_STORAGE_DRIVER_XPERIA_ICS = "usb_storage_xperia_ics.ko";
private final static String STORAGE_DEVICE_PATH = "/dev/block/sda1";
private final static String[] fsTypes = {"vfat"/*, "ntfs" */};
private final static String[] fsTypesInView = {"Filesystem: vfat"/*, "ntfs" */};
private int fsType;
private final static boolean bIsXperiaSeries = android.os.Build.MODEL.equals("LT18i") || // arc s
android.os.Build.MODEL.equals("LT15i") || // arc
android.os.Build.MODEL.equals("SK17i") || // mini pro
android.os.Build.MODEL.equals("ST15i") || // mini
android.os.Build.MODEL.equals("MT15i") || // neo
android.os.Build.MODEL.equals("MT11i") || // neo v
android.os.Build.MODEL.equals("Z1i"); // play
private final static int STATE_SUCCESS = 0;
private final static int STATE_ERROR_MOUNT = -1;
private final static int STATE_ERROR_MODULE = -2;
private final static int STATE_ERROR_MOUNTPOINT = -3;
private Context mContext = this;
ArrayAdapter<String> adapter = null;
TextView tvMountStatus = null;
CheckBox cbCloseOnMount = null;
CheckBox cbReadOnly = null;
NotificationManager notificationManager = null;
SharedPreferences mPrefs = null;
boolean bCloseOnMount = false;
boolean bReadOnly = false;
// inner broadcast receiver for closing self when removing usb storage
private final BroadcastReceiver mOtgReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent != null) {
String action = intent.getAction();
if (action.equals(ACTION_SE_USB_DEVICE_DETACHED) ||
action.equals(UsbManager.ACTION_USB_DEVICE_DETACHED)) {
Main.this.finish();
}
}
}
};
private final OnClickListener btnMountOnClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
if (isMounted()) {
new UnmountStorageTask().execute();
} else {
new MountStorageTask().execute();
}
}
};
// this is only for ArcS
private class CopyKernelDriverTask extends AsyncTask<Void, Void, Void> {
private ProgressDialog dialogCopyingModule = null;
private boolean doCopy = false;
@Override
protected void onPreExecute() {
try {
if (bIsXperiaSeries) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
storage_km = FN_STORAGE_DRIVER_XPERIA_ICS;
} else {
storage_km = FN_STORAGE_DRIVER_XPERIA;
}
}
openFileInput(storage_km);
Log.d(TAG, "driver file exist, skip copy process..");
} catch (FileNotFoundException e) {
doCopy = true;
dialogCopyingModule = ProgressDialog.show(mContext, "", getString(R.string.str_initial), false);
}
}
@Override
protected Void doInBackground(Void... params) {
if (doCopy) {
try {
Log.d(TAG, "driver file not found, copy..");
writeToStream(getAssets().open(storage_km), openFileOutput(storage_km, 0));
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(Void result) {
if (dialogCopyingModule != null) {
dialogCopyingModule.dismiss();
}
}
}
private int doMount() {
int ret = STATE_ERROR_MOUNT;
boolean driverLoaded = false;
List<String> response = null;
do {
try {
if (bIsXperiaSeries) {
response = Root.executeSU("lsmod");
if (response != null) {
for (String r : response) {
if (r.contains("usb_storage")) {
Log.d(TAG, "kernel module already loaded");
driverLoaded = true;
}
}
}
// load kernel module if needed
if (!driverLoaded) {
response = Root.executeSU("insmod " + mContext.getFileStreamPath(storage_km));
if (response != null) {
Log.d(TAG, "Error loading kernel module :" + response);
ret = STATE_ERROR_MODULE;
break;
}
}
}
// check mount point
File mountDirectory = new File(MOUNT_PATH);
if (mountDirectory.exists() && !mountDirectory.isDirectory()) {
response = Root.executeSU("rm " + MOUNT_PATH);
if (response != null) {
Log.d(TAG, "Error deleting file @ mount point :" + response);
ret = STATE_ERROR_MOUNTPOINT;
break;
}
}
if (!mountDirectory.exists()) {
response = Root.executeSU("mkdir " + MOUNT_PATH);
if (response != null) {
Log.d(TAG, "Error creating mount point :" + response);
ret = STATE_ERROR_MOUNTPOINT;
break;
}
}
// if STORAGE_DEVICE_PATH does not exist, wait for it
File deviceFile = new File(STORAGE_DEVICE_PATH);
int count = 0;
while (!deviceFile.exists() && count < 5) {
Thread.sleep(1000);
count++;
}
// do real mount
response = Root.executeSU("mount -r " + (bReadOnly ? "" : "-w") + " -o utf8 -t " + fsTypes[fsType] + " " + STORAGE_DEVICE_PATH + " " + MOUNT_PATH);
if (response != null) {
Log.d(TAG, "Error mounting usb storage :" + response);
Root.executeSU("rmdir " + MOUNT_PATH);
break;
}
ret = STATE_SUCCESS;
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
} while (false);
return ret;
}
public static boolean doUnmount() {
List<String> response = null;
boolean bSuccess = false;
do {
try {
response = Root.executeSU("umount " + MOUNT_PATH);
if (response != null) {
Log.d(TAG, "Error umount usb storage :" + response);
if (isStorageExist()) {
// if there's no storage inserted, do not break here
break;
}
}
response = Root.executeSU("rmdir " + MOUNT_PATH);
if (response != null) {
Log.d(TAG, "Error removing mount point :" + response);
break;
}
// TODO: option for user
// response = Root.executeSU("rmmod " + storage_km);
// if (response != null) {
// Log.d(TAG, "Error disabling kernel module :" + response);
// break;
// }
bSuccess = true;
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
} while (false);
return bSuccess;
}
private void updateUI() {
if (isMounted()) {
tvMountStatus.setText(getResources().getString(R.string.str_mounted_path, MOUNT_PATH));
tvMountStatus.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.usb_android_connected), null, null);
} else {
tvMountStatus.setText(R.string.str_unmounted);
tvMountStatus.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.usb_android), null, null);
}
}
private void showNotification() {
Intent notifyIntent = new Intent(mContext, UnmountService.class);
// notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent appIntent = PendingIntent.getService(mContext, 0, notifyIntent, 0);
Notification notification = new Notification();
notification.icon = R.drawable.notification;
notification.tickerText = getResources().getString(R.string.str_mounted_notify);
// notification.defaults = Notification.DEFAULT_ALL;
notification.flags |= Notification.FLAG_NO_CLEAR;
notification.setLatestEventInfo(mContext, getResources().getString(R.string.app_name),
getResources().getString(R.string.str_unmount) + " " + MOUNT_PATH, appIntent);
notificationManager.notify(0, notification);
}
private class MountStorageTask extends AsyncTask<Void, Void, Integer> {
private ProgressDialog dialogMounting = null;
@Override
protected void onPreExecute() {
dialogMounting = ProgressDialog.show(mContext, "", getString(R.string.str_mounting), false);
}
@Override
protected Integer doInBackground(Void... params) {
return doMount();
}
@Override
protected void onPostExecute(Integer result) {
if (dialogMounting != null) {
dialogMounting.dismiss();
}
if (result == STATE_SUCCESS) {
showNotification();
} else {
int msgId = R.string.str_err_mount;
if (result == STATE_ERROR_MODULE) {
msgId = R.string.str_err_module;
} else if (result == STATE_ERROR_MOUNTPOINT) {
msgId = R.string.str_err_mountpoint;
}
new AlertDialog.Builder(mContext)
.setMessage(msgId)
.setNeutralButton(android.R.string.ok, null)
.show();
}
if (bCloseOnMount && (result == STATE_SUCCESS)) {
finish();
} else {
updateUI();
}
}
}
private class UnmountStorageTask extends AsyncTask<Void, Void, Boolean> {
private ProgressDialog dialogUnmounting = null;
@Override
protected void onPreExecute() {
dialogUnmounting = ProgressDialog.show(mContext, "", getString(R.string.str_unmounting), false);
}
@Override
protected Boolean doInBackground(Void... params) {
return doUnmount();
}
@Override
protected void onPostExecute(Boolean success) {
if (dialogUnmounting != null) {
dialogUnmounting.dismiss();
}
if (success) {
notificationManager.cancelAll();
} else {
new AlertDialog.Builder(mContext)
.setMessage(R.string.str_err_unmount)
.setNeutralButton(android.R.string.ok, null)
.show();
}
updateUI();
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mPrefs = getSharedPreferences(PREFS_NAME, 0);
bCloseOnMount = mPrefs.getBoolean(PREFS_CLOSE_ON_MOUNT, false);
bReadOnly = mPrefs.getBoolean(PREFS_READ_ONLY, false);
setContentView(R.layout.main);
cbCloseOnMount = (CheckBox) findViewById(R.id.chkbox_hide);
cbCloseOnMount.setChecked(bCloseOnMount);
cbCloseOnMount.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
bCloseOnMount = isChecked;
SharedPreferences.Editor editor = mPrefs.edit();
editor.putBoolean(PREFS_CLOSE_ON_MOUNT, bCloseOnMount);
editor.commit();
}
});
cbReadOnly = (CheckBox) findViewById(R.id.chkbox_readonly);
cbReadOnly.setChecked(bReadOnly);
cbReadOnly.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
bReadOnly = isChecked;
SharedPreferences.Editor editor = mPrefs.edit();
editor.putBoolean(PREFS_READ_ONLY, bReadOnly);
editor.commit();
}
});
tvMountStatus = (TextView) findViewById(R.id.tv_mountstatus);
tvMountStatus.setOnClickListener(btnMountOnClickListener);
adapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_item, fsTypesInView);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner spinner = (Spinner) findViewById(R.id.spinner_fstype);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
fsType = position;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
updateUI();
IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_SE_USB_DEVICE_DETACHED);
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
mContext.registerReceiver(mOtgReceiver, filter);
if (bIsXperiaSeries) {
new CopyKernelDriverTask().execute();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
mContext.unregisterReceiver(mOtgReceiver);
}
//
// Utility function
//
private static boolean isStorageExist() {
return new File(STORAGE_DEVICE_PATH).exists();
}
public static boolean isMounted() {
File dir = new File(MOUNT_PATH);
if (dir.exists() && dir.isDirectory()) {
return true;
} else if (!dir.exists()) {
return false;
}
// path is file ?!
return false;
}
private static void writeToStream(InputStream in, OutputStream out) throws IOException {
byte[] bytes = new byte[2048];
for (int c = in.read(bytes); c != -1; c = in.read(bytes)) {
out.write(bytes, 0, c);
}
in.close();
out.close();
}
}