Skip to content
Closed
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
3 changes: 2 additions & 1 deletion res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Um deinen eigenen Server zu installieren und konfigurieren, schaue bitte hier:</
<string name="app_version">PhotoBackup Version %1$s</string>
<string name="cancel">Abbrechen</string>
<string name="info_conf">Informationen</string>
<string name="journal_backedup">Gespeichert</string>
<string name="journal_backedup">Fertig</string>
<string name="journal_error">Fehler</string>
<string name="journal_noaccess">Starte erst den Dienst um das Protokoll zu sehen</string>
<string name="journal_title">Upload Protokoll</string>
Expand Down Expand Up @@ -44,4 +44,5 @@ Um deinen eigenen Server zu installieren und konfigurieren, schaue bitte hier:</
<string name="toast_serverpassempty">Passwort darf nicht leer sein!</string>
<string name="toast_test_configuration">Server testen</string>
<string name="toast_urisyntaxexception">Die Serveradresse ist ungültig.</string>
<string name="select_folder">Ordner zur Sicherung auswählen</string>
</resources>
2 changes: 2 additions & 0 deletions res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@
<item>@string/not_only_recent_upload</item>
</string-array>

<string-array name="empty_array"/>

</resources>
2 changes: 2 additions & 0 deletions res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,7 @@
<string name="stop_service">Stop the service</string>
<string name="manual_backup_title">Manual backup</string>
<string name="manual_backup_message">Save this picture now!</string>
<string name="select_folder">Select photo folder to backup</string>


</resources>
10 changes: 10 additions & 0 deletions res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@
android:entryValues="@array/pref_upload_values"
android:defaultValue="@string/only_recent_upload" />

<MultiSelectListPreference
android:key="PREF_BUCKETS"
android:title="@string/select_folder"
android:summary=""
android:entries="@array/empty_array"
android:defaultValue="@array/empty_array"
android:enabled="false"
android:entryValues="@array/empty_array"
/>

<PreferenceCategory
android:title="@string/info_conf"
android:key="info_conf">
Expand Down
29 changes: 17 additions & 12 deletions src/fr/s13d/photobackup/PBMediaSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,16 @@ private void sendMedia(final PBMedia media) {
getOkClient().newCall(request).enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.i(LOG_TAG, "Get response with code " + response.code());
if (response.code() == 200) {
sendDidSucceed(media);
} else {
sendDidFail(media, new Throwable(response.message()));
try {
Log.i(LOG_TAG, "Get response with code " + response.code());
if (response.isSuccessful() || response.code() == 409) {
sendDidSucceed(media);
} else {
sendDidFail(media, new Throwable(response.message()));
}
} finally {
response.body().close();
}
response.body().close();
}

@Override
Expand Down Expand Up @@ -163,12 +166,15 @@ public void test() {
getOkClient().newCall(request).enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful() || response.code() == 409) {
testDidSucceed(toast);
} else {
testDidFail(toast, response.message());
try {
if (response.isSuccessful()) {
testDidSucceed(toast);
} else {
testDidFail(toast, response.message());
}
} finally {
response.body().close();
}
response.body().close();
}

@Override
Expand All @@ -195,7 +201,6 @@ private Request makePostRequest(RequestBody requestBody, String pathSuffix) {
return requestBuilder.build();
}


/////////////////////
// Private methods //
/////////////////////
Expand Down
65 changes: 31 additions & 34 deletions src/fr/s13d/photobackup/PBMediaStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public class PBMediaStore {
private static SyncMediaStoreTask syncTask;
private static SharedPreferences picturesPreferences;
private static SharedPreferences.Editor picturesPreferencesEditor;
public static final String PhotoBackupPicturesSharedPreferences = "PhotoBackupPicturesSharedPreferences";
private static PBMediaStoreQueries queries;
private List<PBMediaStoreInterface> interfaces = new ArrayList<>();


Expand All @@ -51,6 +53,7 @@ public PBMediaStore(Context theContext) {
picturesPreferences = context.getSharedPreferences(PBApplication.PB_PICTURES_SHARED_PREFS, Context.MODE_PRIVATE);
picturesPreferencesEditor = picturesPreferences.edit();
picturesPreferencesEditor.apply();
queries = new PBMediaStoreQueries(context);
syncTask=new SyncMediaStoreTask();
}

Expand All @@ -65,6 +68,14 @@ private static void setPicturesPreferencesEditorToNull(){
picturesPreferencesEditor = null;
}

public PBMedia getLastMediaInStore() {
int id = queries.getLastMediaIdInStore();
return getMedia(id);
}

public PBMediaStoreQueries getQueries() {
return queries;
}

public void addInterface(PBMediaStoreInterface storeInterface) {
interfaces.add(storeInterface);
Expand All @@ -83,27 +94,30 @@ public void close() {


public PBMedia getMedia(int id) {

PBMedia media = null;
if (id != 0) {
final Cursor cursor = context.getContentResolver().query(uri, null, "_id = " + id, null, null);
if (cursor != null && cursor.moveToFirst()) {
media = new PBMedia(context, cursor);
if (id == 0) {
return null;
}
Cursor cursor = queries.getMediaById(id);
if (cursor == null) {
Log.e(LOG_TAG, "Photo not returned. Probably filtered by Bucket or deleted");
return null;
}
Integer idCol = cursor.getColumnIndex(MediaStore.Images.Media.BUCKET_ID);
queries.isSelectedBucket(cursor.getString(idCol));

try {
String stateString = picturesPreferences.getString(String.valueOf(media.getId()), PBMedia.PBMediaState.WAITING.name());
media.setState(PBMedia.PBMediaState.valueOf(stateString));
}
catch (Exception e) {
Log.e(LOG_TAG, "Explosion!!");
}
}

if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
media = new PBMedia(context, cursor);
try {
String stateString = picturesPreferences.getString(String.valueOf(media.getId()), PBMedia.PBMediaState.WAITING.name());
media.setState(PBMedia.PBMediaState.valueOf(stateString));
} catch (Exception e) {
Log.e(LOG_TAG, "Explosion!!");
}

if (!cursor.isClosed()) {
cursor.close();
}
return media;
}

Expand All @@ -113,17 +127,6 @@ public List<PBMedia> getMedias() {
}


public PBMedia getLastMediaInStore() {
int id = 0;
final String[] projection = new String[] { "_id" };
final Cursor cursor = context.getContentResolver().query(uri, projection, null, null, "date_added DESC");
if (cursor != null && cursor.moveToFirst()) {
int idColumn = cursor.getColumnIndexOrThrow("_id");
id = cursor.getInt(idColumn);
cursor.close();
}
return getMedia(id);
}


/////////////////////////////////
Expand Down Expand Up @@ -162,13 +165,7 @@ protected Void doInBackground(Void... voids) {
Set<String> inCursor = new HashSet<>();

// Get all pictures on device
final String[] projection = new String[] { "_id", "_data", "date_added" };
Cursor cursor = null;
try {
cursor = context.getContentResolver().query(uri, projection, null, null, "date_added DESC");
} catch(SecurityException e) {
Log.w(LOG_TAG, e.toString());
}
Cursor cursor = queries.getAllMedia();

// loop through them to sync
PBMedia media;
Expand Down
164 changes: 164 additions & 0 deletions src/fr/s13d/photobackup/PBMediaStoreQueries.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/**
* Copyright (C) 2013-2015 Stéphane Péchard.
*
* This file is part of PhotoBackup.
*
* PhotoBackup is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PhotoBackup is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package fr.s13d.photobackup;

import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.text.TextUtils;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Set;

import fr.s13d.photobackup.preferences.PBPreferenceFragment;


public class PBMediaStoreQueries {

private static final String LOG_TAG = "PBMediaStoreQueries";
private static Context context;
private static final Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
private final SharedPreferences prefs;


public PBMediaStoreQueries(Context theContext) {
context = theContext;
prefs = PreferenceManager.getDefaultSharedPreferences(context);
}

public boolean isSelectedBucket(String bucketId) {
Set<String> buckets = prefs.getStringSet(PBPreferenceFragment.PREF_BUCKETS, null);
// User has selected nothing - all buckets ok!
if (buckets == null || buckets.size() == 0) {
return true;
}
Log.d(LOG_TAG, "Checking if " + bucketId + " is selected by user..");

for (String bucket: buckets) {
if(bucket.equals(bucketId)) {
Log.d(LOG_TAG, "Is selected bucket! Continuing");
return true;
}
}
Log.i(LOG_TAG, "Is not in selected buckets - Ignoring");
return false;
}

public Cursor getAllMedia() {
String where;
Cursor cursor = null;
final String[] projection = new String[] { "_id", "_data", "date_added" };
Set<String> buckets = prefs.getStringSet(PBPreferenceFragment.PREF_BUCKETS, null);
if (buckets != null && buckets.size() > 0) {
String args = TextUtils.join(", ", buckets);
where = "bucket_id in (" + args + ")";
} else {
where = null;
}
try {
cursor = context.getContentResolver().query(uri, projection, where, null, "date_added DESC");
} catch(SecurityException e) {
e.printStackTrace();
}
return cursor;
}

public Integer getLastMediaIdInStore() {
int id = 0;
String bucketId = "";
final String[] projection = new String[] { "_id", MediaStore.Images.Media.BUCKET_ID };
final Cursor cursor = context.getContentResolver().query(uri, projection, null, null, "date_added DESC");
if (cursor != null && cursor.moveToFirst()) {
int idColumn = cursor.getColumnIndexOrThrow("_id");
id = cursor.getInt(idColumn);
int bucketColumn = cursor.getColumnIndex(MediaStore.Images.Media.BUCKET_ID);
bucketId = cursor.getString(bucketColumn);
}
closeCursor(cursor);
isSelectedBucket(bucketId);
return id;
}

public Cursor getMediaById(Integer id) {
String localWhere = "_id = " + id;
final Cursor cursor = context.getContentResolver().query(uri, null, localWhere, null, null);
if (cursor != null && cursor.moveToFirst()) {
return cursor;
} else {
return null;
}
}
public LinkedHashMap<String,String> getAllBucketNames() {
// which image properties are we querying
String[] PROJECTION_BUCKET = {
MediaStore.Images.ImageColumns.BUCKET_ID,
MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME,
"count(*) as photo_count"
};
// We want to order the albums by reverse chronological order. We abuse the
// "WHERE" parameter to insert a "GROUP BY" clause into the SQL statement.
// The template for "WHERE" parameter is like:
// SELECT ... FROM ... WHERE (%s)
// and we make it look like:
// SELECT ... FROM ... WHERE (1) GROUP BY 1,(2)
// The "(1)" means true. The "1,(2)" means the first two columns specified
// after SELECT. Note that because there is a ")" in the template, we use
// "(2" to match it.
String BUCKET_GROUP_BY =
"1) GROUP BY 1,(2";

// Get the base URI for the People table in the Contacts content provider.
Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

Cursor cur = context.getContentResolver().query(
images, PROJECTION_BUCKET, BUCKET_GROUP_BY, null, "photo_count desc");

LinkedHashMap<String,String> imageBuckets = new LinkedHashMap<String, String>();
if (cur != null && cur.moveToFirst()) {
String bucket;
String id;
String bucketCount;
int bucketColumn = cur.getColumnIndex(
MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
int bucketIdColumn = cur.getColumnIndex(
MediaStore.Images.Media.BUCKET_ID);
int bucketCountColumn = cur.getColumnIndex("photo_count");

do {
bucket = cur.getString(bucketColumn);
id = cur.getString(bucketIdColumn);
bucketCount = cur.getString(bucketCountColumn);
imageBuckets.put(id, bucket + " (" + bucketCount + ")");
} while (cur.moveToNext());
}
closeCursor(cur);
return imageBuckets;
}

private void closeCursor(Cursor cursor) {
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
}

}
Loading