Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option for selective upload. #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ android {
minSdkVersion 10
targetSdkVersion 10
versionCode 6
versionName "1.5"
versionName "1.6"
}

compileOptions {
Expand Down
18 changes: 12 additions & 6 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

<application
android:allowBackup="true"
Expand All @@ -15,19 +15,25 @@
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SettingsActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
<activity android:name=".ConnectActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
<activity android:name=".SelectionActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
<activity android:name=".NewsActivity" />
</application>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.github.LubikR.synologyuploader;

import java.util.ArrayList;

public class AppNotificationManager {
public interface NotificationListener {
void onNotify(String message);
}

private static final AppNotificationManager instance = new AppNotificationManager();

public static AppNotificationManager getInstance() {
return instance;
}

private ArrayList<NotificationListener> listeners = new ArrayList<NotificationListener>();

private AppNotificationManager() {}

public void notify(String message) {
for (NotificationListener listener : listeners)
listener.onNotify(message);
}

public void addListener(NotificationListener listener) {
listeners.add(listener);
}

public void removeListener(NotificationListener listener) {
listeners.remove(listener);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
import android.content.Intent;
import android.view.KeyEvent;

import com.github.ma1co.openmemories.framework.DisplayManager;
import com.sony.scalar.sysutil.ScalarInput;

public class BaseActivity extends Activity {
public static final String NOTIFICATION_DISPLAY_CHANGED = "NOTIFICATION_DISPLAY_CHANGED";



@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Expand All @@ -32,4 +36,6 @@ protected void setAutoPowerOffMode(boolean enable) {
intent.putExtra("apo_info", mode);
sendBroadcast(intent);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.github.LubikR.synologyuploader;

import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.media.ExifInterface;

public class BitmapUtil {
public static Bitmap fixOrientation(Bitmap bitmap, int orientation) {
int degrees = getExifOrientationDegrees(orientation);
if (bitmap != null && degrees != 0) {
Matrix matrix = new Matrix();
matrix.postRotate(degrees);
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}
return bitmap;
}

public static int getExifOrientationDegrees(int orientation) {
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
return 90;
case ExifInterface.ORIENTATION_ROTATE_180:
return 180;
case ExifInterface.ORIENTATION_ROTATE_270:
return 270;
default:
return 0;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ protected void onResume() {
public void onWifiStateChanged() {
WifiState state = getWifiState();
textView.setText(state.toString());


if (state == WifiState.CONNECTED) {
setKeepWifiOn();
finish();
Expand Down
176 changes: 121 additions & 55 deletions app/src/main/java/com/github/LubikR/synologyuploader/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public class MainActivity extends WifiActivity {
DateFormat formatter = new SimpleDateFormat("ddMMyyyy", Locale.US);

Button btnSettings;
Button btnSelectimages;
Button btnUploadSelected;
Button btnUpload;
TextView statusTextView;
ProgressBar progressBar;
Expand All @@ -51,6 +53,8 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);

btnSettings = (Button) findViewById(R.id.Settings);
btnSelectimages = (Button) findViewById(R.id.btnSelectimages);
btnUploadSelected = (Button) findViewById(R.id.btnUploadselected);
btnUpload = (Button) findViewById(R.id.Upload_Now);
statusTextView = (TextView) findViewById(R.id.textviewStatus);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
Expand All @@ -70,6 +74,15 @@ public void onClick(View view) {
startActivity(intent);
}
});

btnSelectimages.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setKeepWifiOn();
Intent intent = new Intent(MainActivity.this, SelectionActivity.class);
startActivity(intent);
}
});
}

@Override
Expand All @@ -93,6 +106,7 @@ protected void onResume() {
private void checkIfAlreadySet() {
if ((SharedPreferencesManager.read(getString(R.string.port), null)) == null) {
btnUpload.setEnabled(false);
btnUploadSelected.setEnabled(false);
} else {
ip = SharedPreferencesManager.read(getString(R.string.address), null);
port = SharedPreferencesManager.read(getString(R.string.port), null);
Expand All @@ -102,29 +116,43 @@ private void checkIfAlreadySet() {
deleteAfterUpload = SharedPreferencesManager.readBoolean(getString(R.string.chckBoxDelete), false);
debug = SharedPreferencesManager.readBoolean(getString(R.string.chkkBoxLog), false);
btnUpload.setEnabled(true);
if (SelectedImages.selectedimages.isEmpty()){
btnUploadSelected.setEnabled(false);
}
else
btnUploadSelected.setEnabled(true);
}
}

public void uploadSelectedClick(View view) {
UploadPictures uploadPictures = new UploadPictures();
uploadPictures.execute(true);
}


public void uploadNowClick(View view) {
UploadPictures uploadPictures = new UploadPictures();
uploadPictures.execute();
uploadPictures.execute(false);
}

//uploading pictures by Multipart class
class UploadPictures extends AsyncTask<Void, String, Integer> {
class UploadPictures extends AsyncTask<Boolean, String, Integer> {

@Override
protected void onPreExecute() {
setAutoPowerOffMode(false);
publishProgress(getString(R.string.connecting));
btnSettings.setEnabled(false);
btnUpload.setEnabled(false);
btnSelectimages.setEnabled(false);
btnUploadSelected.setEnabled(false);
}

@Override
protected Integer doInBackground(Void... voids) {
protected Integer doInBackground(Boolean... selectedOnly) {
int result = 0;
int count = 0;
boolean flaggedForUpload = false;

MediaManager mediaManager = MediaManager.create(getApplicationContext());
Cursor cursor = mediaManager.queryImages();
Expand Down Expand Up @@ -171,68 +199,98 @@ protected Integer doInBackground(Void... voids) {
String model = DeviceInfo.getInstance().getModel();
String directory = SharedPreferencesManager.read(getString(R.string.settingViewDirectory), null);

if(selectedOnly[0]){
count = SelectedImages.selectedimages.size();
}

while (cursor.moveToNext()) {
i++;

final ImageInfo info = mediaManager.getImageInfo(cursor);
String filename = info.getFilename();
Date date = info.getDate();
Long imageId = info.getImageId();
if (SelectedImages.selectedimages.contains(imageId)){
flaggedForUpload = true;
}

publishProgress("Uploading " + i + " / " + count);
runOnUiThread(new Runnable() {
@Override
public void run() {
int i = 0;
progressBar.setVisibility(ProgressBar.VISIBLE);
try {
FileChannel fc = ((FileInputStream) info.getFullImage()).getChannel();
i = (int) fc.size();
fc.close();
}
catch (IOException e) {
//TODO : Do something with exeption

if(!selectedOnly[0] || flaggedForUpload) {
i++;
publishProgress("Uploading " + i + " / " + count);
runOnUiThread(new Runnable() {
@Override
public void run() {
int i = 0;
progressBar.setVisibility(ProgressBar.VISIBLE);
try {
FileChannel fc = ((FileInputStream) info.getFullImage()).getChannel();
i = (int) fc.size();
fc.close();
} catch (IOException e) {
//TODO : Do something with exeption
}
progressBar.setMax(i);
}
progressBar.setMax(i);
});

MultiPartUpload multipart = new MultiPartUpload(address +
SynologyAPI.uploadAPI, "UTF-8", sid);

multipart.addFormField("api", "SYNO.FileStation.Upload");
multipart.addFormField("version", maxVersionUpload);
if (debug) {
Logger.info(TAG, "UploadVersion=" + maxVersionUpload);
}
});

MultiPartUpload multipart = new MultiPartUpload(address +
SynologyAPI.uploadAPI, "UTF-8", sid);

multipart.addFormField("api", "SYNO.FileStation.Upload");
multipart.addFormField("version", maxVersionUpload);
if (debug) { Logger.info(TAG, "UploadVersion=" + maxVersionUpload); }
multipart.addFormField("method", "upload");
multipart.addFormField("path", "/" + directory + "/" + model + "/" + formatter.format(date));
if (debug) { Logger.info(TAG, "Path=" + "/" + directory + "/" + model + "/" +
formatter.format(date)); }
multipart.addFormField("create_parents", "true");
multipart.addFilePart("file", (FileInputStream) info.getFullImage(), filename, getApplicationContext());
if (debug) { Logger.info(TAG, "Filename=" + filename); }

String json2 = new String(multipart.finish());
String uploadResult = new JSONObject(json2).getString("success");

if (uploadResult.equals("true")) {
if (deleteAfterUpload) {
// Delete uploaded image
ContentResolver resolver = getApplicationContext().getContentResolver();
Uri uri = mediaManager.getImageContentUri();
long id = mediaManager.getImageId(cursor);
AvindexStore.Images.Media.deleteImage(resolver, uri, id);
multipart.addFormField("method", "upload");
multipart.addFormField("path", "/" + directory + "/" + model + "/" + formatter.format(date));
if (debug) {
Logger.info(TAG, "Path=" + "/" + directory + "/" + model + "/" +
formatter.format(date));
}
} else {
String errorCode = new JSONObject(json2).getJSONObject("error").getString("code");
if (debug) { Logger.info(TAG, "Error during upload: " + errorCode); }
throw new HttpException(errorCode);
}
multipart.addFormField("create_parents", "true");
multipart.addFilePart("file", (FileInputStream) info.getFullImage(), filename, getApplicationContext());
if (debug) {
Logger.info(TAG, "Filename=" + filename);
}

String json2 = new String(multipart.finish());
String uploadResult = new JSONObject(json2).getString("success");

if (uploadResult.equals("true")) {
if (deleteAfterUpload) {
// Delete uploaded image
ContentResolver resolver = getApplicationContext().getContentResolver();
Uri uri = mediaManager.getImageContentUri();
long id = mediaManager.getImageId(cursor);
AvindexStore.Images.Media.deleteImage(resolver, uri, id);
}
if (flaggedForUpload){
flaggedForUpload = false;
SelectedImages.selectedimages.remove(imageId);
}
if( !selectedOnly[0] && deleteAfterUpload){
SelectedImages.selectedimages.clear();
}

//Reset progress bar
runOnUiThread(new Runnable() {
@Override
public void run() {
progressBar.setProgress(0);
} else {
String errorCode = new JSONObject(json2).getJSONObject("error").getString("code");
if (debug) {
Logger.info(TAG, "Error during upload: " + errorCode);
}
throw new HttpException(errorCode);
}
});


//Reset progress bar
runOnUiThread(new Runnable() {
@Override
public void run() {
progressBar.setProgress(0);

}

});
}
}
cursor.close();

Expand Down Expand Up @@ -282,6 +340,14 @@ protected void onPostExecute(Integer result) {
}
btnUpload.setEnabled(true);
btnSettings.setEnabled(true);
btnSelectimages.setEnabled(true);
if (SelectedImages.selectedimages.isEmpty()){
btnUploadSelected.setEnabled(false);
}
else {
btnUploadSelected.setEnabled(true);
}

progressBar.setVisibility(ProgressBar.INVISIBLE);
}
}
Expand Down
Loading