Commit 729f5231 authored by Chris Wren's avatar Chris Wren
Browse files

add a broadcast receiver that modifies the settings.

Useful for loosly embedding the photo dreams code into other packages.
Add support for albums on internal storage.

Bug: 8370249
Change-Id: Ibca37c100d7f1f6b87580422591de4d19d726506
parent c37df74e
......@@ -18,6 +18,7 @@ package com.android.dreams.phototable;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import java.io.FileInputStream;
......@@ -58,12 +59,21 @@ public class LocalSource extends CursorPhotoSource {
public Collection<AlbumData> findAlbums() {
log(TAG, "finding albums");
HashMap<String, AlbumData> foundAlbums = new HashMap<String, AlbumData>();
findAlbums(false, foundAlbums);
findAlbums(true, foundAlbums);
log(TAG, "found " + foundAlbums.size() + " items.");
mFoundAlbumIds = foundAlbums.keySet();
return foundAlbums.values();
}
public void findAlbums(boolean internal, HashMap<String, AlbumData> foundAlbums) {
Uri uri = internal ? MediaStore.Images.Media.INTERNAL_CONTENT_URI
: MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] projection = {MediaStore.Images.Media.DATA, MediaStore.Images.Media.BUCKET_ID,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME, MediaStore.Images.Media.DATE_TAKEN};
// This is a horrible hack that closes the where clause and injects a grouping clause.
Cursor cursor = mResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection, null, null, null);
Cursor cursor = mResolver.query(uri, projection, null, null, null);
if (cursor != null) {
cursor.moveToPosition(-1);
......@@ -76,7 +86,7 @@ public class LocalSource extends CursorPhotoSource {
log(TAG, "can't find the ID column!");
} else {
while (cursor.moveToNext()) {
String id = TAG + ":" + cursor.getString(bucketIndex);
String id = constructId(internal, cursor.getString(bucketIndex));
AlbumData data = foundAlbums.get(id);
if (foundAlbums.get(id) == null) {
data = new AlbumData();
......@@ -105,11 +115,11 @@ public class LocalSource extends CursorPhotoSource {
}
}
cursor.close();
}
log(TAG, "found " + foundAlbums.size() + " items.");
mFoundAlbumIds = foundAlbums.keySet();
return foundAlbums.values();
}
public static String constructId(boolean internal, String bucketId) {
return TAG + ":" + bucketId + (internal ? ":i" : "");
}
@Override
......@@ -120,8 +130,7 @@ public class LocalSource extends CursorPhotoSource {
MediaStore.Images.Media.BUCKET_ID, MediaStore.Images.Media.BUCKET_DISPLAY_NAME};
String selection = MediaStore.Images.Media.BUCKET_ID + " = '" + data.albumId + "'";
data.cursor = mResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection, selection, null, null);
data.cursor = mResolver.query(data.uri, projection, selection, null, null);
}
@Override
......@@ -169,12 +178,21 @@ public class LocalSource extends CursorPhotoSource {
protected Collection<ImageData> findImages(int howMany) {
log(TAG, "finding images");
LinkedList<ImageData> foundImages = new LinkedList<ImageData>();
boolean internalFirst = mRNG.nextInt(2) == 0; // filp a coin to be fair
findImages(internalFirst, howMany, foundImages);
findImages(!internalFirst, howMany - foundImages.size(), foundImages);
log(TAG, "found " + foundImages.size() + " items.");
return foundImages;
}
protected void findImages(boolean internal, int howMany, LinkedList<ImageData> foundImages ) {
Uri uri = internal ? MediaStore.Images.Media.INTERNAL_CONTENT_URI
: MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] projection = {MediaStore.Images.Media.DATA, MediaStore.Images.Media.ORIENTATION,
MediaStore.Images.Media.BUCKET_ID, MediaStore.Images.Media.BUCKET_DISPLAY_NAME};
String selection = "";
for (String id : getFoundAlbums()) {
if (mSettings.isAlbumEnabled(id)) {
if (isInternalId(id) == internal && mSettings.isAlbumEnabled(id)) {
String[] parts = id.split(":");
if (parts.length > 1) {
if (selection.length() > 0) {
......@@ -185,11 +203,9 @@ public class LocalSource extends CursorPhotoSource {
}
}
if (selection.isEmpty()) {
return foundImages;
return;
}
Cursor cursor = mResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection, selection, null, null);
Cursor cursor = mResolver.query(uri, projection, selection, null, null);
if (cursor != null) {
int dataIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
......@@ -203,6 +219,7 @@ public class LocalSource extends CursorPhotoSource {
} else {
while (foundImages.size() < howMany && cursor.moveToNext()) {
ImageData data = unpackImageData(cursor, null);
data.uri = uri;
foundImages.offer(data);
mLastPosition = cursor.getPosition();
}
......@@ -216,8 +233,10 @@ public class LocalSource extends CursorPhotoSource {
cursor.close();
}
log(TAG, "found " + foundImages.size() + " items.");
return foundImages;
}
private boolean isInternalId(String id) {
return id.endsWith("i");
}
@Override
......
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.dreams.phototable;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
public class PhotoDreamSettingsReceiver extends BroadcastReceiver {
private static final String TAG = "PhotoDreamSettingsReceiver";
private static final String LOCAL_AUTHORITY = "media";
private static final String INTERNAL = "internal";
public static final String ACTION_ADD_ALBUM = "add";
public static final String ACTION_REMOVE_ALBUM = "remove";
public static final String EXTRA_ALBUMS = "albums";
@Override
public void onReceive(Context context, Intent intent) {
AlbumSettings settings[] = {
AlbumSettings.getAlbumSettings(
context.getSharedPreferences(FlipperDreamSettings.PREFS_NAME, 0)),
AlbumSettings.getAlbumSettings(
context.getSharedPreferences(PhotoTableDreamSettings.PREFS_NAME, 0))
};
boolean shown = ACTION_ADD_ALBUM.equals(intent.getAction());
ArrayList<String> albumUris = intent.getStringArrayListExtra(EXTRA_ALBUMS);
for (String albumUriString: albumUris) {
Uri albumUri = Uri.parse(albumUriString);
String type = albumUri.getEncodedAuthority();
List<String> path = albumUri.getPathSegments();
String albumId = null;
if (LOCAL_AUTHORITY.equals(type)) {
if (path.size() > 3) {
albumId = LocalSource.constructId(INTERNAL.equals(path.get(0)), path.get(3));
}
} else {
if (path.size() > 1) {
albumId = PicasaSource.constructId(path.get(1));
}
}
for (int idx = 0; idx < settings.length; idx++) {
settings[idx].setAlbumEnabled(albumId, shown);
}
}
}
}
......@@ -23,6 +23,7 @@ import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.net.Uri;
import android.util.Log;
import java.io.BufferedInputStream;
......@@ -54,6 +55,7 @@ public abstract class PhotoSource {
protected String albumId;
protected Cursor cursor;
protected int position;
protected Uri uri;
InputStream getStream(int longSide) {
return PhotoSource.this.getStream(this, longSide);
......
......@@ -354,7 +354,7 @@ public class PicasaSource extends CursorPhotoSource {
log(TAG, "can't find the ID column!");
} else {
while (cursor.moveToNext()) {
String id = TAG + ":" + cursor.getString(idIndex);
String id = constructId(cursor.getString(idIndex));
String user = (userIndex >= 0 ? cursor.getString(userIndex) : "-1");
String type = (typeIndex >= 0 ? cursor.getString(typeIndex) : "none");
boolean isPosts = (typeIndex >= 0 && PICASA_POSTS_TYPE.equals(type));
......@@ -369,12 +369,12 @@ public class PicasaSource extends CursorPhotoSource {
if (isPosts) {
log(TAG, "replacing " + id + " with " + PICASA_POSTS_TYPE);
id = TAG + ":" + PICASA_POSTS_TYPE + ":" + user;
id = constructId(PICASA_POSTS_TYPE + ":" + user);
}
if (isUpload) {
log(TAG, "replacing " + id + " with " + PICASA_UPLOAD_TYPE);
id = TAG + ":" + PICASA_UPLOAD_TYPE + ":" + user;
id = constructId(PICASA_UPLOAD_TYPE + ":" + user);
}
String thumbnailUrl = null;
......@@ -425,6 +425,10 @@ public class PicasaSource extends CursorPhotoSource {
return foundAlbums.values();
}
public static String constructId(String serverId) {
return TAG + ":" + serverId;
}
@Override
protected InputStream getStream(ImageData data, int longSide) {
InputStream is = null;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment