Skip to content

Commit

Permalink
Show Edited Files
Browse files Browse the repository at this point in the history
Signed-off-by: GauthamAsir <[email protected]>
  • Loading branch information
GauthamAsir committed May 13, 2020
1 parent eaddbf4 commit fbb01e2
Show file tree
Hide file tree
Showing 9 changed files with 447 additions and 11 deletions.
6 changes: 5 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ dependencies {

implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

Expand All @@ -43,6 +43,10 @@ dependencies {
//Picasso
implementation 'com.squareup.picasso:picasso:2.71828'

//Image Cropper
api 'com.theartofdev.edmodo:android-image-cropper:2.8.0'

//Apache Commons-Io to calculate file size
implementation 'commons-io:commons-io:2.6'

}
15 changes: 9 additions & 6 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="a.gautham.neweditor">

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Expand All @@ -12,7 +13,9 @@
android:requestLegacyExternalStorage="true"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
android:theme="@style/AppTheme"
tools:targetApi="q">
<activity android:name=".EditedFiles" />

<provider
android:name="androidx.core.content.FileProvider"
Expand All @@ -21,22 +24,22 @@
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
android:resource="@xml/file_paths" />
</provider>

<activity android:name=".MainActivity" android:label="@string/app_name">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name=".EditorActivity"
android:label="Editor"
android:windowSoftInputMode="adjustResize"/>

android:windowSoftInputMode="adjustResize" />
<activity
android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
android:theme="@style/Base.Theme.AppCompat" />
Expand Down
122 changes: 122 additions & 0 deletions app/src/main/java/a/gautham/neweditor/EditedFiles.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package a.gautham.neweditor;

import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

import a.gautham.neweditor.helper.FileAdapter;
import a.gautham.neweditor.helper.FileModel;

public class EditedFiles extends AppCompatActivity {

private ListView listView;
private ProgressBar progressBar;
private TextView no_files_tv;
private List<FileModel> list = new ArrayList<>();
private File file;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edited_files);

listView = findViewById(R.id.listView);
progressBar = findViewById(R.id.progressBar);
no_files_tv = findViewById(R.id.no_files_tv);

}

@Override
protected void onResume() {
super.onResume();

String path = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator +
"Pictures" + File.separator + "GMemes Creator";

file = new File(path);
if (file.exists())
loadFiles();
else
noFiles();

}

private void loadFiles() {

progressBar.setVisibility(View.VISIBLE);
no_files_tv.setVisibility(View.GONE);
listView.setVisibility(View.VISIBLE);

new Thread(new Runnable() {
@Override
public void run() {

File[] values = file.listFiles();
list.clear();

if (values != null && values.length > 0) {

Arrays.sort(values, new Comparator<File>() {
@Override
public int compare(File o1, File o2) {
return Long.compare((o2).lastModified(), (o1).lastModified());
}
});
for (File file1 : values) {
if (!file1.isDirectory()) {
FileModel fileModel = new FileModel(
file1.getName(),
FileUtils.byteCountToDisplaySize(file1.length()),
file1);
list.add(fileModel);
}
}

runOnUiThread(new Runnable() {
@Override
public void run() {
if (list.size() > 0) {
FileAdapter fileAdapter = new FileAdapter(EditedFiles.this, list);
listView.setAdapter(fileAdapter);
progressBar.setVisibility(View.GONE);
} else {
noFiles();
}

}
});

} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
noFiles();
}
});
}

}
}).start();

}

private void noFiles() {
progressBar.setVisibility(View.GONE);
no_files_tv.setVisibility(View.VISIBLE);
listView.setVisibility(View.GONE);
}

}
54 changes: 50 additions & 4 deletions app/src/main/java/a/gautham/neweditor/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
package a.gautham.neweditor;

import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.app.ActivityManager;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import java.util.Objects;

public class MainActivity extends AppCompatActivity {

private static final int REQUEST_PERMISSIONS = 1234;
private static final String[] PERMISSIONS = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -20,6 +33,39 @@ public void edit(View view) {
}

public void edited_items(View view) {
Toast.makeText(this, "W.I.P", Toast.LENGTH_SHORT).show();

if (!arePermissionDenied()) {
startActivity(new Intent(getApplicationContext(), EditedFiles.class));
return;
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && arePermissionDenied()) {
requestPermissions(PERMISSIONS, REQUEST_PERMISSIONS);
}
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_PERMISSIONS && grantResults.length > 0) {
if (arePermissionDenied()) {
// Clear Data of Application, So that it can request for permissions again
((ActivityManager) Objects.requireNonNull(this.getSystemService(ACTIVITY_SERVICE))).clearApplicationUserData();
recreate();
} else {
startActivity(new Intent(getApplicationContext(), EditedFiles.class));
}
}
}

private boolean arePermissionDenied() {

for (String permissions : PERMISSIONS) {
if (ActivityCompat.checkSelfPermission(getApplicationContext(), permissions) != PackageManager.PERMISSION_GRANTED) {
return true;
}
}
return false;
}

}
96 changes: 96 additions & 0 deletions app/src/main/java/a/gautham/neweditor/helper/FileAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package a.gautham.neweditor.helper;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.FileProvider;

import com.squareup.picasso.Picasso;

import java.util.List;

import a.gautham.neweditor.R;

public class FileAdapter extends ArrayAdapter<FileModel> {

public FileAdapter(@NonNull Context context, List<FileModel> arrayList) {
super(context, 0, arrayList);
}

@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull final ViewGroup parent) {

final FileModel fileModel = getItem(position);

if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.edited_items, parent, false);
}

ImageView file_thumbnail = convertView.findViewById(R.id.file_thumbnail);
ImageView delete_file = convertView.findViewById(R.id.delete_file);
ImageView share_file = convertView.findViewById(R.id.share_file);
TextView file_name = convertView.findViewById(R.id.file_name);
TextView file_size = convertView.findViewById(R.id.file_size);

assert fileModel != null;
Picasso.get().load(fileModel.getFile()).into(file_thumbnail);

file_name.setText(fileModel.getName());
file_size.setText(fileModel.getSize());

delete_file.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

if (fileModel.getFile().exists() && fileModel.getFile().delete())
remove(fileModel);
}
});

share_file.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Uri uri = FileProvider.getUriForFile(
parent.getContext(),
"a.gautham.neweditor.provider",
fileModel.getFile());

Intent shareIntent = new Intent(Intent.ACTION_SEND);
if (fileModel.getName().endsWith(".jpeg")) {
shareIntent.setType("image/jpg");
} else {
shareIntent.setType("image/png");
}
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
Intent chooser = Intent.createChooser(shareIntent, "Share image");

List<ResolveInfo> resInfoList = parent.getContext().getPackageManager()
.queryIntentActivities(chooser, PackageManager.MATCH_DEFAULT_ONLY);

for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
parent.getContext().grantUriPermission(packageName, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}

parent.getContext().startActivity(chooser);

}
});

return convertView;

}
}
40 changes: 40 additions & 0 deletions app/src/main/java/a/gautham/neweditor/helper/FileModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package a.gautham.neweditor.helper;

import java.io.File;

public class FileModel {

private String name;
private String size;
private File file;

public FileModel(String name, String size, File file) {
this.name = name;
this.size = size;
this.file = file;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getSize() {
return size;
}

public void setSize(String size) {
this.size = size;
}

public File getFile() {
return file;
}

public void setFile(File file) {
this.file = file;
}
}
Loading

0 comments on commit fbb01e2

Please sign in to comment.