Skip to content

Commit

Permalink
Added Drag and Drop feature
Browse files Browse the repository at this point in the history
Drag JSON file and drop it into the app to open it
  • Loading branch information
SlaVcE14 committed Jan 27, 2024
1 parent e3ac4be commit 2e5a5dd
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 21 deletions.
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
<activity android:name="com.sjapps.about.AboutActivity"/>
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenSize"
android:configChanges="orientation|screenSize|smallestScreenSize|screenLayout"
android:exported="true">
<meta-data android:name="android.app.shortcuts"
android:resource="@xml/shortcuts" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.OPEN_DOCUMENT"/>

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Expand Down
12 changes: 5 additions & 7 deletions app/src/main/java/com/sjapps/jsonlist/FileSystem.java
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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();
Expand All @@ -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();
Expand Down
104 changes: 91 additions & 13 deletions app/src/main/java/com/sjapps/jsonlist/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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;


Expand All @@ -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() {
Expand Down Expand Up @@ -101,14 +110,70 @@ 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());
}
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() {
Expand Down Expand Up @@ -168,6 +233,7 @@ private void initialize() {
dim_bg.bringToFront();
menu.bringToFront();
menuBtn.bringToFront();
dropTarget = findViewById(R.id.dropTarget);
}

private void open_closeMenu() {
Expand Down Expand Up @@ -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(){
Expand Down Expand Up @@ -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);
}
Expand Down
34 changes: 34 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,40 @@
</RelativeLayout>


<RelativeLayout
android:id="@+id/dropTarget"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:backgroundTintMode="add"
android:layout_margin="10dp"
android:alpha="0"

>

<View
android:id="@+id/dropTargetBackground"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?colorOnBackground"
android:alpha=".5"
/>

<TextView
android:id="@+id/dropTargetText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="30dp"

android:text="@string/drop_json_file_here"
android:textColor="#ffffff"

android:layout_centerInParent="true"
android:layout_alignParentBottom="true"
/>

</RelativeLayout>





Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
<string name="app_name">Json List</string>
<string name="open_file">Open file</string>
<string name="back">Back</string>
<string name="drop_json_file_here">Drop JSON file here</string>
<string name="this_is_not_json_file">This is not JSON file</string>
</resources>

0 comments on commit 2e5a5dd

Please sign in to comment.