fileBrower.java 23 KB

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