fileBrower.java 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. package epson.print;
  2. import android.app.Activity;
  3. import android.app.AlertDialog;
  4. import android.content.Context;
  5. import android.content.DialogInterface;
  6. import android.content.Intent;
  7. import android.net.Uri;
  8. import android.os.Bundle;
  9. import android.support.p000v4.app.NotificationCompat;
  10. import android.text.InputFilter;
  11. import android.util.Log;
  12. import android.view.ContextMenu;
  13. import android.view.Menu;
  14. import android.view.MenuItem;
  15. import android.view.View;
  16. import android.widget.AdapterView;
  17. import android.widget.EditText;
  18. import android.widget.ListView;
  19. import android.widget.Toast;
  20. import com.box.androidsdk.content.requests.BoxRequestsMetadata;
  21. import com.epson.iprint.prtlogger.PrintLog;
  22. import com.fasterxml.jackson.core.JsonPointer;
  23. import java.io.File;
  24. import java.io.IOException;
  25. import java.util.ArrayList;
  26. import java.util.Collections;
  27. import java.util.Comparator;
  28. import java.util.List;
  29. import epson.common.Utils;
  30. import epson.print.Util.Photo;
  31. public class fileBrower extends ActivityIACommon implements CommonDefine {
  32. private static final int Menu_Create = 1;
  33. private static final int Menu_Delete = 1;
  34. private static final int Menu_Rename = 2;
  35. private static final int RESULT_RUNTIMEPERMMISSION = 1;
  36. private String RootPath = CommonDefine.SLASH;
  37. private String TopPath = CommonDefine.MOUNT_DIR;
  38. private File currentDirectory = new File(TopPath);
  39. private List<String> directoryEntries = new ArrayList();
  40. private List<IconifiedText> directotyEntries_new_v2 = new ArrayList();
  41. private ListView list;
  42. private AdapterView.AdapterContextMenuInfo listItemIndex;
  43. public void onCreate(Bundle bundle) {
  44. super.onCreate(bundle);
  45. setContentView(R.layout.file_browser);
  46. if (!new File(TopPath).isDirectory()) {
  47. String str = DEFAULT_DIR;
  48. int lastIndexOf = str.lastIndexOf(CommonDefine.MOUNT_DIR);
  49. if (lastIndexOf != -1) {
  50. str = CommonDefine.SLASH + str.substring(lastIndexOf + 5, str.length()) + CommonDefine.SLASH;
  51. } else if (str.lastIndexOf(CommonDefine.SLASH) != str.length()) {
  52. str = str + CommonDefine.SLASH;
  53. }
  54. TopPath = str;
  55. currentDirectory = new File(TopPath);
  56. }
  57. setActionBar(TopPath, true);
  58. list = (ListView) findViewById(R.id.filelist);
  59. list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  60. public void onItemClick(AdapterView<?> adapterView, View view, int i, long j) {
  61. onListItemClick((ListView) adapterView, view, i, j);
  62. }
  63. });
  64. list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
  65. public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long j) {
  66. if (i == 0) {
  67. if (((String) directoryEntries.get(i)).equals("..")) {
  68. Log.v("select", (String) directoryEntries.get(i));
  69. return true;
  70. }
  71. fileBrower filebrower = fileBrower.this;
  72. filebrower.registerForContextMenu(filebrower.list);
  73. }
  74. if (i <= 2) {
  75. return false;
  76. }
  77. fileBrower filebrower2 = fileBrower.this;
  78. filebrower2.registerForContextMenu(filebrower2.list);
  79. return false;
  80. }
  81. });
  82. list.setBackgroundColor(getResources().getColor(R.color.all_white));
  83. if (ActivityRequestPermissions.isRuntimePermissionSupported()) {
  84. String[] strArr = {"android.permission.WRITE_EXTERNAL_STORAGE"};
  85. ActivityRequestPermissions.Permission permission = new ActivityRequestPermissions.Permission(strArr[0], new String[]{getString(R.string.permission_dialog_title), getString(R.string.permission_dialog_title)}, new String[]{ActivityRequestPermissions.DialogParameter.setMessage2((Context) this, getString(R.string.permission_dialog_message_storage)), ActivityRequestPermissions.DialogParameter.setMessage3A((Context) this, getString(R.string.permission_dialog_message_storage), getString(R.string.permission_function_storage))});
  86. if (!ActivityRequestPermissions.checkPermission(this, strArr)) {
  87. ActivityRequestPermissions.requestPermission((Activity) this, permission, 1);
  88. return;
  89. }
  90. }
  91. startBrowse();
  92. }
  93. private void startBrowse() {
  94. if (Utils.isMediaMounted()) {
  95. browseToRoot();
  96. return;
  97. }
  98. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  99. builder.setMessage(getString(R.string.str_no_sdcard)).setTitle(R.string.unknow_error_title).setCancelable(false).setPositiveButton(getString(R.string.str_exit_btn), new DialogInterface.OnClickListener() {
  100. public void onClick(DialogInterface dialogInterface, int i) {
  101. finish();
  102. }
  103. });
  104. builder.create().show();
  105. }
  106. protected void onActivityResult(int i, int i2, Intent intent) {
  107. super.onActivityResult(i, i2, intent);
  108. if (i == 1) {
  109. if (i2 != -1) {
  110. onBackPressed();
  111. } else {
  112. startBrowse();
  113. }
  114. }
  115. }
  116. public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) {
  117. super.onCreateContextMenu(contextMenu, view, contextMenuInfo);
  118. contextMenu.setHeaderTitle(R.string.str_edit_bookmark);
  119. contextMenu.add(0, 1, 0, R.string.str_delete);
  120. contextMenu.add(0, 2, 0, R.string.str_rename);
  121. }
  122. public boolean onContextItemSelected(MenuItem menuItem) {
  123. listItemIndex = (AdapterView.AdapterContextMenuInfo) menuItem.getMenuInfo();
  124. if (listItemIndex == null) {
  125. return true;
  126. }
  127. switch (menuItem.getItemId()) {
  128. case 1:
  129. final File file = new File(currentDirectory.getAbsolutePath() + directoryEntries.get(listItemIndex.position));
  130. final File file2 = currentDirectory;
  131. C22354 r2 = new DialogInterface.OnClickListener() {
  132. public void onClick(DialogInterface dialogInterface, int i) {
  133. try {
  134. if (!file.isDirectory() || file.listFiles().length <= 0) {
  135. file.delete();
  136. } else {
  137. deleteSubfile(file);
  138. }
  139. browseTo(file2);
  140. } catch (SecurityException e) {
  141. e.printStackTrace();
  142. } catch (IOException e2) {
  143. e2.printStackTrace();
  144. }
  145. }
  146. };
  147. C22365 r0 = new DialogInterface.OnClickListener() {
  148. public void onClick(DialogInterface dialogInterface, int i) {
  149. }
  150. };
  151. if (!file.isFile()) {
  152. new AlertDialog.Builder(this).setIcon(R.drawable.delete_file).setTitle(getString(R.string.delete_file)).setPositiveButton(getString(R.string.str_no), r0).setNegativeButton(getString(R.string.str_yes), r2).show();
  153. break;
  154. } else {
  155. new AlertDialog.Builder(this).setIcon(R.drawable.delete_file).setTitle(getString(R.string.delete_file)).setPositiveButton(getString(R.string.str_no), r0).setNegativeButton(getString(R.string.str_yes), r2).show();
  156. break;
  157. }
  158. case 2:
  159. final File file3 = new File(currentDirectory.getAbsolutePath() + directoryEntries.get(listItemIndex.position));
  160. final File file4 = currentDirectory;
  161. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  162. final EditText editText = new EditText(this);
  163. editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(64)});
  164. String name = file3.getName();
  165. final String suffix = Utils.getSuffix(name);
  166. if (file3.isFile()) {
  167. name = Utils.getPreffix(name);
  168. }
  169. editText.setText(name);
  170. builder.setCancelable(false);
  171. builder.setTitle(R.string.str_rename);
  172. builder.setIcon(R.drawable.info_image);
  173. builder.setView(editText);
  174. builder.setPositiveButton(R.string.str_ok, new DialogInterface.OnClickListener() {
  175. public void onClick(DialogInterface dialogInterface, int i) {
  176. String trim = editText.getText().toString().trim();
  177. if (trim == null || trim.length() <= 0) {
  178. Toast.makeText (getApplicationContext(), R.string.str_error_Filename, 1).show();
  179. return;
  180. }
  181. try {
  182. if (file3.isFile() && suffix.length() > 0) {
  183. trim = trim + CommonDefine.DOT + suffix;
  184. }
  185. File file = new File(file3.getParent() + CommonDefine.SLASH + trim);
  186. if (!fileBrower.isAvailableFileName(trim)) {
  187. Toast.makeText (getApplicationContext(), R.string.str_error_Filename, 1).show();
  188. } else if (file.exists()) {
  189. if (!file3.getPath().equals(file.getPath())) {
  190. Toast.makeText (getApplicationContext(), R.string.str_error_Filename, 1).show();
  191. }
  192. } else if (file3.renameTo(file)) {
  193. browseTo(file4);
  194. } else {
  195. Toast.makeText (getApplicationContext(), R.string.str_error_Filename, 1).show();
  196. }
  197. } catch (Exception unused) {
  198. Toast.makeText (getApplicationContext(), R.string.error, 1).show();
  199. }
  200. }
  201. });
  202. builder.setNegativeButton(R.string.str_cancel, new DialogInterface.OnClickListener() {
  203. public void onClick(DialogInterface dialogInterface, int i) {
  204. dialogInterface.cancel();
  205. }
  206. });
  207. builder.show();
  208. break;
  209. }
  210. return true;
  211. }
  212. public static boolean isAvailableFileName(String str) {
  213. if (str == null || str.length() == 0) {
  214. return false;
  215. }
  216. char[] cArr = {'<', '>', ':', '*', '?', '\"', JsonPointer.SEPARATOR, '\\', '|', 165};
  217. for (char indexOf : cArr) {
  218. if (str.indexOf(indexOf) != -1) {
  219. return false;
  220. }
  221. }
  222. if (str.startsWith(CommonDefine.DOT)) {
  223. return false;
  224. }
  225. return true;
  226. }
  227. public void onBackPressed() {
  228. try {
  229. if (!upOneLevel()) {
  230. finish();
  231. }
  232. } catch (NullPointerException unused) {
  233. super.onBackPressed();
  234. }
  235. }
  236. private void createNewFolder() {
  237. Log.v("path current", currentDirectory.getAbsolutePath());
  238. final EditText editText = new EditText(this);
  239. editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(64)});
  240. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  241. final String absolutePath = currentDirectory.getAbsolutePath();
  242. C22398 r3 = new DialogInterface.OnClickListener() {
  243. public void onClick(DialogInterface dialogInterface, int i) {
  244. String obj = editText.getText().toString();
  245. if (isNameFolder(obj)) {
  246. File file = new File(absolutePath + CommonDefine.SLASH + obj);
  247. Log.v("create file", absolutePath + CommonDefine.SLASH + obj);
  248. if (!file.exists()) {
  249. try {
  250. if (file.mkdirs()) {
  251. browseTo(new File(absolutePath));
  252. } else {
  253. fileBrower.mMessage();
  254. }
  255. } catch (IllegalArgumentException unused) {
  256. fileBrower.mMessage();
  257. Log.v("exception", "1");
  258. } catch (Exception unused2) {
  259. fileBrower.mMessage();
  260. Log.v("exception", ExifInterface.GPS_MEASUREMENT_2D);
  261. }
  262. } else {
  263. fileBrower.mMessage();
  264. }
  265. } else {
  266. fileBrower.mMessage();
  267. }
  268. }
  269. };
  270. builder.setTitle("Question").setIcon(R.drawable.new_folder).setView(editText).setTitle(getString(R.string.add_new_folder)).setPositiveButton(R.string.str_ok, r3).setNegativeButton(R.string.str_cancel, new DialogInterface.OnClickListener() {
  271. public void onClick(DialogInterface dialogInterface, int i) {
  272. Log.v("NamVH", "Cant make the new file");
  273. }
  274. }).show();
  275. }
  276. private void mMessage() {
  277. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  278. builder.setMessage(getString(R.string.str_error_Filename)).setCancelable(false).setPositiveButton(getString(R.string.str_ok), new DialogInterface.OnClickListener() {
  279. public void onClick(DialogInterface dialogInterface, int i) {
  280. dialogInterface.cancel();
  281. }
  282. });
  283. builder.create().show();
  284. }
  285. private boolean isNameFolder(String str) {
  286. return str != null && !str.equals("") && !str.substring(0, 1).equals(CommonDefine.DOT) && str.indexOf(CommonDefine.SLASH) == -1 && str.length() <= 64;
  287. }
  288. public boolean onOptionsItemSelected(MenuItem menuItem) {
  289. if (menuItem.getItemId() == 1) {
  290. createNewFolder();
  291. }
  292. return super.onOptionsItemSelected(menuItem);
  293. }
  294. private void browseToRoot() {
  295. browseTo(new File(TopPath));
  296. }
  297. private boolean upOneLevel() {
  298. String parent = currentDirectory.getParent();
  299. if (parent == null) {
  300. return true;
  301. }
  302. if (!new File(parent).canRead()) {
  303. return false;
  304. }
  305. browseTo(currentDirectory.getParentFile());
  306. return true;
  307. }
  308. private boolean isFileText(String str) {
  309. return str != null && Utils.checkMimeType(str, CommonDefine.TEXT_TYPE);
  310. }
  311. private boolean isFilePdf(String str) {
  312. return str != null && Utils.checkMimeType(str, "application/pdf");
  313. }
  314. private boolean getFileType(String str) {
  315. if (str == null) {
  316. return false;
  317. }
  318. return Utils.isGConvertFile(str) || isFilePdf(str) || Photo.IsFilePhoto(str) || isFileText(str);
  319. }
  320. private boolean getFileSize(String str) {
  321. File file = new File(str);
  322. if (!file.exists() || file.isDirectory()) {
  323. return false;
  324. }
  325. return (!Photo.IsFilePhoto(str) || file.length() < 3145728) ? true : true;
  326. }
  327. private boolean getFileShow(String str) {
  328. File file = new File(str);
  329. if (!file.exists()) {
  330. return false;
  331. }
  332. if (!file.isDirectory() && !getFileType(str)) {
  333. return false;
  334. }
  335. return true;
  336. }
  337. private void browseTo(File file) {
  338. if (file.isDirectory()) {
  339. currentDirectory = file;
  340. fill(file.listFiles());
  341. return;
  342. }
  343. try {
  344. if (Photo.IsFilePhoto(file.getAbsolutePath())) {
  345. startPhotoPreview(file);
  346. return;
  347. }
  348. if (!isFilePdf(file.getAbsolutePath())) {
  349. if (!Utils.isGConvertFile(file.getAbsolutePath())) {
  350. if (isFileText(file.getAbsolutePath())) {
  351. Intent intent = new Intent(this, WebviewActivity.class);
  352. intent.putExtra("from", 3);
  353. intent.putExtra("android.intent.extra.TEXT", "file:/" + file.getAbsolutePath());
  354. startActivity(intent);
  355. return;
  356. }
  357. Toast.makeText(getBaseContext(), getString(R.string.file_size_notsupport), 0).show();
  358. return;
  359. }
  360. }
  361. if (getFileSize(file.getAbsolutePath())) {
  362. Intent intent2 = new Intent(this, ActivityDocsPrintPreview.class);
  363. intent2.putExtra("from", 3);
  364. intent2.putExtra(CommonDefine.SEND_DOCUMENT_VIEW, file.getAbsolutePath());
  365. intent2.putExtra("print_log", getPrintLog(file));
  366. startActivity(intent2);
  367. return;
  368. }
  369. Log.v("file size", ">max");
  370. Toast.makeText(getBaseContext(), getString(R.string.file_size_notsupport), 0).show();
  371. } catch (Exception e) {
  372. e.printStackTrace();
  373. }
  374. }
  375. private void startPhotoPreview(File file) {
  376. Intent intent = new Intent(this, ActivityViewImageSelect.class);
  377. intent.putExtra("from", 3);
  378. intent.putExtra(ActivityViewImageSelect.PARAMS_KEY_FROM_EPSON, true);
  379. intent.setAction("android.intent.action.SEND");
  380. intent.putExtra("android.intent.extra.STREAM", Uri.parse(file.getAbsolutePath()));
  381. if (Utils.checkMimeType(file.getAbsolutePath(), "image/png")) {
  382. intent.setType("image/png");
  383. }
  384. intent.putExtra(CommonDefine.STYPE, true);
  385. intent.putExtra("print_log", getPrintLog(file));
  386. startActivity(intent);
  387. }
  388. private PrintLog getPrintLog(File file) {
  389. PrintLog printLog = new PrintLog();
  390. printLog.uiRoute = 2;
  391. printLog.originalFileExtension = PrintLog.getFileExtension(file);
  392. return printLog;
  393. }
  394. private void fill(File[] fileArr) {
  395. Log.v(NotificationCompat.CATEGORY_CALL, "fill function");
  396. directoryEntries.clear();
  397. directotyEntries_new_v2.clear();
  398. if (fileArr != null) {
  399. Log.v("abc", String.valueOf(fileArr.length));
  400. int length = currentDirectory.getAbsolutePath().length();
  401. for (File file : fileArr) {
  402. if (!file.getName().substring(0, 1).equals(CommonDefine.DOT)) {
  403. if (isFilePdf(file.getAbsolutePath())) {
  404. getFileShow(file.getAbsolutePath());
  405. }
  406. if (!file.getAbsolutePath().equals("/mnt/secure") && !file.getAbsolutePath().equals("/mnt/asec") && !file.getAbsolutePath().equals("/mnt/.lfs") && !file.getAbsolutePath().equals("/mnt/obb") && ((!currentDirectory.getAbsolutePath().equals(CommonDefine.MNT) || !file.isDirectory() || file.listFiles() != null) && getFileShow(file.getAbsolutePath()))) {
  407. directoryEntries.add(file.getAbsolutePath().substring(length));
  408. directotyEntries_new_v2.add(createIcon(file.getAbsolutePath()));
  409. }
  410. }
  411. }
  412. Collections.sort(directoryEntries, new StringComparator());
  413. Collections.sort(directotyEntries_new_v2, new IconifiedText());
  414. }
  415. setTitle(currentDirectory.getAbsolutePath());
  416. Log.v("getparent", currentDirectory.getParent());
  417. if (!currentDirectory.getParent().equals(RootPath)) {
  418. Log.v("compare", "not equals");
  419. Log.v("getparent currentDirectory", currentDirectory.getParent());
  420. directoryEntries.add(0, "..");
  421. directotyEntries_new_v2.add(0, new IconifiedText("..", getResources().getDrawable(R.drawable.parent_folder), true));
  422. }
  423. IconifiedTextListAdapter iconifiedTextListAdapter = new IconifiedTextListAdapter(this);
  424. iconifiedTextListAdapter.setListItems(directotyEntries_new_v2);
  425. list.setAdapter(iconifiedTextListAdapter);
  426. }
  427. class StringComparator implements Comparator<String> {
  428. public StringComparator() {
  429. }
  430. public int compare(String str, String str2) {
  431. if (str == null || str2 == null) {
  432. return 0;
  433. }
  434. if (str.equalsIgnoreCase(CommonDefine.DOT)) {
  435. return -1;
  436. }
  437. if (str2.equalsIgnoreCase(CommonDefine.DOT)) {
  438. return 1;
  439. }
  440. return str.compareToIgnoreCase(str2);
  441. }
  442. }
  443. protected void onListItemClick(ListView listView, View view, int i, long j) {
  444. Log.v("onclick", String.valueOf(i));
  445. String str = directoryEntries.get(i);
  446. Log.v(BoxRequestsMetadata.UpdateItemMetadata.BoxMetadataUpdateTask.PATH, str);
  447. if (str.equals(CommonDefine.DOT)) {
  448. browseTo(new File(TopPath));
  449. } else if (str.equals("..")) {
  450. upOneLevel();
  451. } else if (!str.startsWith(CommonDefine.DOT)) {
  452. browseTo(new File(currentDirectory.getAbsolutePath() + CommonDefine.SLASH + directotyEntries_new_v2.get(i).getText()));
  453. }
  454. }
  455. private IconifiedText createIcon(String str) {
  456. File file = new File(str);
  457. if (file.isDirectory()) {
  458. return new IconifiedText(file.getName(), getResources().getDrawable(R.drawable.folder), true);
  459. }
  460. if (Photo.IsFilePhoto(str)) {
  461. return new IconifiedText(file.getName(), getResources().getDrawable(R.drawable.image), false);
  462. }
  463. if (Utils.isGConvertFile(str) || isFilePdf(str) || isFileText(str)) {
  464. return new IconifiedText(file.getName(), getResources().getDrawable(R.drawable.file), false);
  465. }
  466. return new IconifiedText(file.getName(), getResources().getDrawable(R.drawable.icon), false);
  467. }
  468. public boolean onCreateOptionsMenu(Menu menu) {
  469. super.onCreateOptionsMenu(menu);
  470. menu.add(0, 1, 0, getString(R.string.new_folder)).setIcon(R.drawable.new_folder).setShortcut('0', 'f');
  471. return true;
  472. }
  473. private void deleteSubfile(File file) throws SecurityException, IOException {
  474. for (File file2 : file.listFiles()) {
  475. if (!fileEnableDelete(file2)) {
  476. deleteSubfile(file2);
  477. }
  478. file2.delete();
  479. }
  480. file.delete();
  481. if (file.exists()) {
  482. throw new IOException("Unable to delete directory hierarchy \"" + file.getAbsolutePath() + "\"");
  483. }
  484. }
  485. private boolean fileEnableDelete(File file) {
  486. if (file.isFile()) {
  487. return true;
  488. }
  489. if (!file.isDirectory() || file.list().length != 0) {
  490. return false;
  491. }
  492. return true;
  493. }
  494. }