diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 0cdb808..f0595c7 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -18,12 +18,13 @@ + diff --git a/app/src/main/java/com/sjapps/jsonlist/FileSystem.java b/app/src/main/java/com/sjapps/jsonlist/FileSystem.java index 2f8719c..c0a0639 100644 --- a/app/src/main/java/com/sjapps/jsonlist/FileSystem.java +++ b/app/src/main/java/com/sjapps/jsonlist/FileSystem.java @@ -1,14 +1,16 @@ package com.sjapps.jsonlist; +import android.content.Context; import android.content.res.AssetFileDescriptor; import android.net.Uri; + import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import java.io.BufferedReader; -import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStream; import java.io.InputStreamReader; import java.nio.file.Path; public class FileSystem { @@ -21,7 +23,7 @@ public static JsonArray loadDataToJsonArray(JsonElement data) { return data.getAsJsonArray(); } - public static String LoadDataFromFile(MainActivity mainActivity, Uri uri) { + public static String LoadDataFromFile(Context context, Uri uri, InputStream inputStream, AssetFileDescriptor fileDescriptor) { if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { String path = uri.getPath(); @@ -34,18 +36,14 @@ public static String LoadDataFromFile(MainActivity mainActivity, Uri uri) { StringBuilder sb = new StringBuilder(); try { - FileInputStream inputStream = (FileInputStream) mainActivity.getContentResolver().openInputStream(uri); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); - - AssetFileDescriptor fileDescriptor = mainActivity.getContentResolver().openAssetFileDescriptor(uri , "r"); - long currentBytes = 0; long fileSize = fileDescriptor.getLength(); String line; while ((line = reader.readLine()) != null) { sb.append(line); currentBytes += line.length(); - mainActivity.updateProgress((int)((currentBytes/(float)fileSize)*100)); + ((MainActivity) context).updateProgress((int)((currentBytes/(float)fileSize)*100)); } fileDescriptor.close(); diff --git a/app/src/main/java/com/sjapps/jsonlist/MainActivity.java b/app/src/main/java/com/sjapps/jsonlist/MainActivity.java index a75fc33..05ffda1 100644 --- a/app/src/main/java/com/sjapps/jsonlist/MainActivity.java +++ b/app/src/main/java/com/sjapps/jsonlist/MainActivity.java @@ -11,13 +11,18 @@ import android.app.Activity; import android.content.Context; +import android.content.ClipData; import android.content.Intent; +import android.content.res.AssetFileDescriptor; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.transition.AutoTransition; import android.transition.TransitionManager; import android.util.Log; +import android.util.TypedValue; +import android.view.DragAndDropPermissions; +import android.view.DragEvent; import android.view.View; import android.view.ViewGroup; import android.view.animation.Animation; @@ -31,6 +36,7 @@ import android.widget.LinearLayout; import android.widget.ListView; import android.widget.ProgressBar; +import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; @@ -40,13 +46,15 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; + import com.sjapps.about.AboutActivity; import com.sjapps.adapters.ListAdapter; import com.sjapps.jsonlist.java.JsonData; import com.sjapps.jsonlist.java.ListItem; import com.sjapps.library.customdialog.BasicDialog; - +import java.io.IOException; +import java.io.InputStream; import java.util.ArrayList; @@ -68,6 +76,7 @@ public class MainActivity extends AppCompatActivity { AutoTransition autoTransition = new AutoTransition(); Handler handler = new Handler(); Thread readFileThread; + RelativeLayout dropTarget; @Override protected void onResume() { @@ -101,7 +110,7 @@ protected void onCreate(Bundle savedInstanceState) { dim_bg.setOnClickListener(view -> open_closeMenu()); - Intent intent = getIntent(); + Intent intent = getIntent(); Log.d(TAG, "onCreate: " + intent); if (Intent.ACTION_VIEW.equals(intent.getAction())) { ReadFile(intent.getData()); @@ -109,6 +118,62 @@ protected void onCreate(Bundle savedInstanceState) { if (intent.getAction().equals("android.intent.action.OPEN_FILE")){ ImportFromFile(); } + + dropTarget.setOnDragListener((v, event) -> { + + TextView dropTargetTxt = v.findViewById(R.id.dropTargetText); + View dropTargetBackground = v.findViewById(R.id.dropTargetBackground); + + switch (event.getAction()) { + case DragEvent.ACTION_DRAG_STARTED: + dropTarget.setAlpha(1); + return true; + + case DragEvent.ACTION_DRAG_ENTERED: + if (event.getClipDescription().hasMimeType("application/json")) { + dropTargetBackground.setBackgroundColor(setColor(R.attr.colorPrimary)); + dropTargetBackground.setAlpha(.8f); + }else { + dropTargetTxt.setText(R.string.this_is_not_json_file); + dropTargetBackground.setBackgroundColor(setColor(R.attr.colorError)); + } + return true; + + case DragEvent.ACTION_DRAG_EXITED: + dropTargetTxt.setText(R.string.drop_json_file_here); + dropTargetBackground.setBackgroundColor(setColor(R.attr.colorOnBackground)); + dropTargetBackground.setAlpha(.5f); + return true; + + case DragEvent.ACTION_DRAG_ENDED: + dropTargetTxt.setText(R.string.drop_json_file_here); + dropTargetBackground.setBackgroundColor(setColor(R.attr.colorOnBackground)); + dropTarget.setAlpha(0); + return true; + + case DragEvent.ACTION_DROP: + if (!event.getClipDescription().hasMimeType("application/json")) + return false; + if (readFileThread != null && readFileThread.isAlive()) { + Snackbar.make(getWindow().getDecorView(),"Loading file in progress, try again later", BaseTransientBottomBar.LENGTH_SHORT).show(); + return false; + } + + ClipData.Item item = event.getClipData().getItemAt(0); + + DragAndDropPermissions dropPermissions = null; + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) + dropPermissions = requestDragAndDropPermissions(event); + + ReadFile(item.getUri()); + + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N && dropPermissions != null) + dropPermissions.release(); + + return true; + } + return false; + }); } private void OpenAbout() { @@ -168,6 +233,7 @@ private void initialize() { dim_bg.bringToFront(); menu.bringToFront(); menuBtn.bringToFront(); + dropTarget = findViewById(R.id.dropTarget); } private void open_closeMenu() { @@ -314,21 +380,27 @@ void ReadFile(Uri uri){ } loadingStarted("Reading file"); + try { + InputStream inputStream = getContentResolver().openInputStream(uri); + AssetFileDescriptor fileDescriptor = getContentResolver().openAssetFileDescriptor(uri , "r"); - readFileThread = new Thread(() -> { - String Data = FileSystem.LoadDataFromFile(MainActivity.this, uri); + readFileThread = new Thread(() -> { - if (Data == null) { - Log.d(TAG, "ReadFile: null data"); - return; - } - handler.post(() -> { - LoadData(Data); - }); + String Data = FileSystem.LoadDataFromFile(MainActivity.this, uri, inputStream, fileDescriptor); - }); - readFileThread.start(); + if (Data == null) { + Log.d(TAG, "ReadFile: null data"); + return; + } + handler.post(() -> { + LoadData(Data); + }); + }); + readFileThread.start(); + } catch (IOException e) { + throw new RuntimeException(e); + } } void loadingStarted(){ @@ -379,6 +451,12 @@ void loadingFinished(boolean isFinished){ } + int setColor(int resid){ + TypedValue typedValue = new TypedValue(); + getTheme().resolveAttribute(resid, typedValue, true); + return typedValue.data; + } + public static void setAnimation(Context context, @NonNull View view, @AnimRes int animationRes) { setAnimation(context,view,animationRes,null); } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 18320d3..67736c5 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -164,6 +164,40 @@ + + + + + + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5d8256b..7ea89ad 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -2,4 +2,6 @@ Json List Open file Back + Drop JSON file here + This is not JSON file \ No newline at end of file