Skip to content

Commit

Permalink
Merge pull request #6 from daktak/development
Browse files Browse the repository at this point in the history
0.4
  • Loading branch information
daktak authored Jul 12, 2016
2 parents 048f455 + f8ed27c commit 6f68f0e
Show file tree
Hide file tree
Showing 7 changed files with 785 additions and 14 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
applicationId "org.afhdownloader"
minSdkVersion 16
targetSdkVersion 24
versionCode 4
versionName "0.3"
versionCode 5
versionName "0.4"
}
buildTypes {
release {
Expand Down
14 changes: 12 additions & 2 deletions app/src/main/java/org/afhdownloader/Download.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;


/**
Expand Down Expand Up @@ -94,6 +95,15 @@ public String parseUrl(String url) {
urls.add(link.ownText());
urls.add(link.attr("href"));
}
//set title
String head = getString(R.string.app_name);
try {
Elements h1s = doc.select(getString(R.string.head_selector));
head = h1s.get(0).ownText();
} catch (Exception e) {
Log.d(LOGTAG,"Unable to find heading");
}
urls.add(head);

} catch (Throwable t) {
Log.e(LOGTAG,t.getMessage());
Expand Down Expand Up @@ -253,9 +263,9 @@ protected void onPostExecute(String s) {
public void download(String url, String desc, String title, String filename) {
SharedPreferences mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

String exten = "zip";
String exten = "/";

if (url.endsWith(exten)) {
if (!url.endsWith(exten)) {

Log.d(LOGTAG, "Downloading: " + url);
boolean external = mySharedPreferences.getBoolean("prefExternal", false);
Expand Down
69 changes: 60 additions & 9 deletions app/src/main/java/org/afhdownloader/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import android.Manifest;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
Expand Down Expand Up @@ -39,9 +41,12 @@ public class MainActivity extends AppCompatActivity
private static final int REQUEST_PREFS = 99;
private static final int RC_EXT_WRITE =1;
private static final int RC_EXT_READ=2;
private static final int YES_NO_CALL=80;
public static MainActivity instance = null;

public ArrayList<String> md5check = new ArrayList<String>();
public ArrayList<String> names = new ArrayList<String>();
public ArrayList<String> urls = new ArrayList<String>();
public String directory;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -238,12 +243,12 @@ public void writeFile(String name, String body){
}

public void setList(List<String> values) {
ArrayList<String> names = new ArrayList<String>();
SharedPreferences mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
String directory = mySharedPreferences.getString("prefDirectory",Environment.DIRECTORY_DOWNLOADS).trim();
directory = mySharedPreferences.getString("prefDirectory",Environment.DIRECTORY_DOWNLOADS).trim();
boolean external = mySharedPreferences.getBoolean("prefExternal",false);
ArrayList<String> md5check = new ArrayList<String>();

md5check = new ArrayList<String>();
String md5_ext = getString(R.string.md5_ext);
final String md5_calc_ext = getString(R.string.md5calc_ext);
if (external){
directory = Environment.DIRECTORY_DOWNLOADS;
}
Expand All @@ -262,9 +267,10 @@ public void setList(List<String> values) {
}
}

getSupportActionBar().setTitle(values.get(values.size()-1));

//for each returned value - filename and url
for (int j = 0; j < values.size(); j+=2) {
for (int j = 0; j < values.size()-1; j+=2) {
String md5val = "";
String url = values.get(j+1).trim();
String name = values.get(j).trim();
Expand All @@ -283,16 +289,16 @@ public void setList(List<String> values) {
for (int k = 0; k < file.length; k++) {

if (name.equals(file[k].getName())) {
String md5 = readFile(name + ".md5");
String md5 = readFile(name + md5_ext);
if (!md5.isEmpty()) {
String md5calc = readFile(name+".calc.md5");
String md5calc = readFile(name+md5_calc_ext);
if (md5calc.isEmpty()) {
md5calc = MD5.calculateMD5(file[k]);
}
if (md5calc.equalsIgnoreCase(md5)) {
md5val = "Y";
//cache this result
writeFile(name+".calc.md5", md5calc);
writeFile(name+md5_calc_ext, md5calc);
} else {
md5val = "N";
//don't cache, in the event the file is still downloading
Expand Down Expand Up @@ -321,6 +327,38 @@ public void setList(List<String> values) {

// Set the ArrayAdapter as the ListView's adapter.
mainListView.setAdapter( listAdapter );
SwipeDismissListViewTouchListener touchListener =
new SwipeDismissListViewTouchListener(
mainListView,
new SwipeDismissListViewTouchListener.DismissCallbacks() {
@Override
public boolean canDismiss(int position) {
boolean dis = true;
if (md5check.get(position).isEmpty()) { dis = false; };
return dis;
}

@Override
public void onDismiss(ListView listView, int[] reverseSortedPositions) {
for (int position : reverseSortedPositions) {
final int pos = position;
DialogInterface.OnClickListener yesListener = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
File direct = new File(Environment.getExternalStorageDirectory() + "/" + directory+"/"+names.get(pos));
Log.d(LOGTAG, "Delete " + direct.getName());
if (direct.exists()&&direct.isFile()) { direct.delete(); }
File md5file = new File(getFilesDir(), names.get(pos)+md5_calc_ext );
if (md5file.exists()&&md5file.isFile()) { md5file.delete(); }
if (MainActivity.instance != null) {
run(MainActivity.instance);
}
}
};
message_dialog_yes_no(getString(R.string.delete) + " " + names.get(pos)+"?" , yesListener);
}
}
});
mainListView.setOnTouchListener(touchListener);

mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
Expand All @@ -343,6 +381,19 @@ public void onItemClick(AdapterView<?> parent, final View view, int position, lo
});
}

public void message_dialog_yes_no (String msg, DialogInterface.OnClickListener yesListener) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

builder.setMessage(msg)
.setCancelable(false)
.setPositiveButton(getString(R.string.yes), yesListener)
.setNegativeButton(getString(R.string.no), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}})
.show();
}

/**
* Executes commands as root user
* @author http://muzikant-android.blogspot.com/2011/02/how-to-get-root-access-and-execute.html
Expand Down
Loading

0 comments on commit 6f68f0e

Please sign in to comment.