Skip to content

Commit

Permalink
Release 0.9
Browse files Browse the repository at this point in the history
  • Loading branch information
dobestler committed Nov 29, 2016
1 parent 7c49a23 commit 6b7bb15
Show file tree
Hide file tree
Showing 15 changed files with 141 additions and 184 deletions.
16 changes: 9 additions & 7 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 24
buildToolsVersion "24.0.0"

compileSdkVersion 25
buildToolsVersion "25.0.0"
defaultConfig {
applicationId "com.home.grabber"
minSdkVersion 20
targetSdkVersion 24
targetSdkVersion 25
versionCode 1
versionName "1.0"
versionName "0.9"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
Expand All @@ -20,11 +22,11 @@ android {
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support:design:24.2.1'
compile 'com.android.support:appcompat-v7:25.0.0'
compile 'com.android.support:design:25.0.0'
testCompile 'junit:junit:4.12'
}
Binary file added app/releases/app-debug-0.9.apk
Binary file not shown.
4 changes: 2 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
android:theme="@android:style/Theme.Dialog">
<activity
android:name="com.home.grabber.GrabberActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
android:theme="@style/Theme.Transparent">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
Expand Down
112 changes: 58 additions & 54 deletions app/src/main/java/com/home/grabber/DownloadCompleteReceiver.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
package com.home.grabber;

import android.app.DownloadManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.os.ParcelFileDescriptor;
import android.util.Log;
import android.webkit.MimeTypeMap;
import android.widget.Toast;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import static android.content.Context.NOTIFICATION_SERVICE;

public class DownloadCompleteReceiver extends BroadcastReceiver {

private static final String TAG = DownloadCompleteReceiver.class.getSimpleName();
private static final int NOTIFICATION_ID = 980000053;

public DownloadCompleteReceiver() {
}
Expand Down Expand Up @@ -48,9 +49,21 @@ public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Download successful. extension=" + MimeTypeMap.getSingleton().getExtensionFromMimeType(type));

// rename file with proper file extension
addProperFileExtension(context, uriString, type, manager, downloadId);

// FIXME notification with VIEW intent
File f1 = new File(uriString);
String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(type);
File f2 = new File(f1.getPath(), f1.getName() + "." + extension);

// boolean success = renameFile(f1.getName(), f2.getName());
// File file;
// if (success) {
// file = f2;
// } else {
// GrabberActivity.showToast(context, "Failed to rename the downloaded file to " + f2.getName());
// file = f1;
// }
// triggerMediaScan(context, file);
File file = f1;
showNotification(context, file.getName());
} else {
Toast.makeText(context, "Download failed", Toast.LENGTH_LONG).show();
Log.w(TAG, "Download failed");
Expand All @@ -60,61 +73,52 @@ public void onReceive(Context context, Intent intent) {
Log.w(TAG, "Download failed");
}

if (c != null) {
c.close();
}
c.close();
}
}

private void addProperFileExtension(final Context context, final String uriString, final String type, final DownloadManager manager, long downloadId) {
File f1 = new File(uriString);
File f2 = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/" +
f1.getName() + "." + MimeTypeMap.getSingleton().getExtensionFromMimeType(type));
private void triggerMediaScan(final Context context, final File file) {
//Broadcast the Media Scanner Intent to trigger it
// context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
// Uri.parse("file://" + Environment.getExternalStorageDirectory())));
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file));
context.sendBroadcast(intent);
}

ParcelFileDescriptor f1Fd = null;
InputStream f1InStream = null;
OutputStream f2OutStream = null;
try {
f1Fd = manager.openDownloadedFile(downloadId);
f1InStream = new FileInputStream(f1Fd.getFileDescriptor());
f2OutStream = new FileOutputStream(f2);
private void showNotification(final Context context, final String filename) {
Log.d(TAG, "showNotification | filename = " + filename);

byte[] buffer = new byte[1024];
int length;
// Open folder
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse(Environment.getExternalStoragePublicDirectory(GrabberActivity.DIRECTORY).getPath());
intent.setDataAndType(uri, "resource/folder");

while ((length = f1InStream.read(buffer)) > 0) {
f2OutStream.write(buffer, 0, length);
}
PendingIntent pIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), intent, 0);
Notification n = new Notification.Builder(context)
.setContentTitle("Download successful")
.setContentText("Open Download directory")
.setStyle(new Notification.BigTextStyle().bigText("Open Download directory\nFilename: " + filename))
.setSmallIcon(R.mipmap.ic_stat_teddy_bear_machine_white)
.setContentIntent(pIntent)
.setAutoCancel(true).build();

f2OutStream.flush();
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);

manager.remove(downloadId);
notificationManager.notify(NOTIFICATION_ID, n);
}

} catch (IOException e) {
Toast.makeText(context, "Renaming failed.", Toast.LENGTH_LONG).show();
Log.e(TAG, "Renaming failed", e);
} finally {
try {
if (f1InStream != null) {
f1InStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (f1Fd != null) {
f1Fd.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (f2OutStream != null) {
f2OutStream.close();
}
} catch (IOException e) {
e.printStackTrace();
private static boolean renameFile(String oldName, String newName) {
File dir = Environment.getExternalStoragePublicDirectory(GrabberActivity.DIRECTORY);
if (dir.exists()) {
File from = new File(dir, oldName);
File to = new File(dir, newName);
Log.d(TAG, "renameFile " + from + " -> " + to);
if (from.exists()) {
return from.renameTo(to);
}
}
return false;
}
}
Loading

0 comments on commit 6b7bb15

Please sign in to comment.