|
|
@@ -0,0 +1,140 @@
|
|
|
+package com.usai.redant.rautils.fileViewer;
|
|
|
+
|
|
|
+import android.content.Context;
|
|
|
+import android.content.Intent;
|
|
|
+import android.net.Uri;
|
|
|
+import android.support.v7.app.ActionBar;
|
|
|
+import android.support.v7.app.AppCompatActivity;
|
|
|
+import android.os.Bundle;
|
|
|
+import android.util.Log;
|
|
|
+import android.view.MenuItem;
|
|
|
+import android.view.View;
|
|
|
+import android.widget.AdapterView;
|
|
|
+import android.widget.ListView;
|
|
|
+
|
|
|
+import com.usai.redant.rautils.R;
|
|
|
+import com.usai.redant.rautils.utils.FileManager;
|
|
|
+import com.usai.redant.rautils.utils.RAUtil;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+
|
|
|
+public class FileViewerActivity extends AppCompatActivity {
|
|
|
+
|
|
|
+ final static String TAG = "FileViewer";
|
|
|
+
|
|
|
+ private final static String File_Key = "File_Key";
|
|
|
+ public static void start(Context context, File file) {
|
|
|
+ if (context == null) {
|
|
|
+ Log.d(TAG, "start fileviewer context is null");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (file == null || !file.exists() || file.isFile()) {
|
|
|
+ Log.d(TAG, "start fileviewer file is null or file not exist or file is not a directory ");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ Intent intent = new Intent(context, FileViewerActivity.class);
|
|
|
+ intent.putExtra(File_Key, file.getAbsolutePath());
|
|
|
+
|
|
|
+ context.startActivity(intent);
|
|
|
+ }
|
|
|
+
|
|
|
+ private ListView mListView;
|
|
|
+ private File mFile;
|
|
|
+ private FileViewerAdapter mAdapter;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onCreate(Bundle savedInstanceState) {
|
|
|
+ super.onCreate(savedInstanceState);
|
|
|
+ setContentView(R.layout.file_viewer_activity);
|
|
|
+
|
|
|
+ ActionBar actionBar = getSupportActionBar();
|
|
|
+ if (actionBar != null) {
|
|
|
+ actionBar.setHomeButtonEnabled(true);
|
|
|
+ actionBar.setDisplayHomeAsUpEnabled(true);
|
|
|
+ }
|
|
|
+
|
|
|
+ Intent intent = getIntent();
|
|
|
+ if (intent != null) {
|
|
|
+ String filePath = intent.getStringExtra(File_Key);
|
|
|
+ if (filePath != null) {
|
|
|
+ mFile = new File(filePath);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (savedInstanceState != null) {
|
|
|
+ String filePath = savedInstanceState.getString(File_Key);
|
|
|
+ if (filePath != null) {
|
|
|
+ mFile = new File(filePath);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ mAdapter = new FileViewerAdapter(this,mFile);
|
|
|
+
|
|
|
+ mListView = findViewById(R.id.file_list_view);
|
|
|
+ mListView.setAdapter(mAdapter);
|
|
|
+ mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
|
|
+
|
|
|
+ File file = (File) mAdapter.getItem(position);
|
|
|
+ clickFileCell(file);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onSaveInstanceState(Bundle outState) {
|
|
|
+ super.onSaveInstanceState(outState);
|
|
|
+
|
|
|
+ if (mFile != null) {
|
|
|
+ outState.putString(File_Key, mFile.getAbsolutePath());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
+ switch (item.getItemId()) {
|
|
|
+ case android.R.id.home: {
|
|
|
+ finish();
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return super.onOptionsItemSelected(item);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void clickFileCell(File file) {
|
|
|
+ if (file != null && file.exists()) {
|
|
|
+
|
|
|
+ if (file.isDirectory()) {
|
|
|
+
|
|
|
+ start(this,file);
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ viewFile(file);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void viewFile(File file) {
|
|
|
+ if (file != null && file.exists() && file.isFile()) {
|
|
|
+
|
|
|
+ String name = file.getName();
|
|
|
+ String oldPath = file.getAbsolutePath();
|
|
|
+ String newDir = FileManager.SDCardRoot() + RAUtil.getApplicationName(getApplicationContext());
|
|
|
+ File dirF = new File(newDir);
|
|
|
+ if (!dirF.exists()) {
|
|
|
+ dirF.mkdirs();
|
|
|
+ }
|
|
|
+
|
|
|
+ String newPath = newDir + File.separator + name;
|
|
|
+ FileManager.copyFile(oldPath, newPath);
|
|
|
+
|
|
|
+ FileManager.openFile(getApplicationContext(), "com.usai.apex.apexdriversi.fileprovider", new File(newPath));
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|