Permalink
Browse files

add view recent reads: read perfect viewer history.xml, need rooted d…

…evice

better pinch zoom
  • Loading branch information...
1 parent d9aaa82 commit 5f51f601a09c7ece59288d510a757fb600776b38 @ghk committed Jul 4, 2011
View
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="utf-8"?>
+<menu xmlns:android="http://schemas.android.com/apk/res/android">
+ <item android:id="@+id/recent"
+ android:title="Recent Comics" />
+ <item android:id="@+id/continue_read"
+ android:title="Continue Read Last Comics" />
+</menu>
@@ -24,10 +24,14 @@
import android.graphics.Camera;
import android.graphics.Matrix;
import android.util.AttributeSet;
+import android.view.MotionEvent;
+import android.view.ScaleGestureDetector;
import android.view.View;
+import android.view.View.OnTouchListener;
import android.view.animation.Transformation;
import android.widget.Gallery;
import android.widget.ImageView;
+import android.widget.TextView;
public class CoverFlow extends Gallery {
@@ -52,6 +56,13 @@
*/
private int mMaxZoom = -100;
+ private int mSpacing = 0;
+
+ private static float MAX_ALLOWED_ZOOM = -200.0f;
+ private static float MIN_ALLOWED_ZOOM = -400.0f;
+
+ private ScaleGestureDetector gestureDetector;
+
/**
* The Centre of the Coverflow
@@ -61,16 +72,22 @@
public CoverFlow(Context context) {
super(context);
this.setStaticTransformationsEnabled(true);
+ setZoom(getZoom());
+ gestureDetector = new ScaleGestureDetector(context, new ScaleGestureListener());
}
public CoverFlow(Context context, AttributeSet attrs) {
super(context, attrs);
this.setStaticTransformationsEnabled(true);
+ setZoom(getZoom());
+ gestureDetector = new ScaleGestureDetector(context, new ScaleGestureListener());
}
public CoverFlow(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.setStaticTransformationsEnabled(true);
+ setZoom(getZoom());
+ gestureDetector = new ScaleGestureDetector(context, new ScaleGestureListener());
}
/**
@@ -113,12 +130,28 @@ public void setMaxZoom(int maxZoom) {
public void setZoom(float zoom){
+ if(zoom > MAX_ALLOWED_ZOOM)
+ zoom = MAX_ALLOWED_ZOOM;
+ if(zoom < MIN_ALLOWED_ZOOM)
+ zoom = MIN_ALLOWED_ZOOM;
mZoom = zoom;
+ int newSpacing = (int)(((zoom - MAX_ALLOWED_ZOOM) / MAX_ALLOWED_ZOOM) * 90) + 20;
+ setSpacing(newSpacing);
}
public float getZoom(){
return mZoom;
}
+
+ @Override
+ public void setSpacing(int spacing) {
+ mSpacing = spacing;
+ super.setSpacing(spacing);
+ }
+
+ public int getSpacing(){
+ return mSpacing;
+ }
/**
* Get the Centre of the Coverflow
@@ -220,4 +253,32 @@ private void transformImageBitmap(ImageView child, Transformation t,
imageMatrix.postTranslate((imageWidth / 2), (imageHeight / 2));
mCamera.restore();
}
+
+
+ @Override
+ public boolean onTouchEvent(MotionEvent event) {
+ gestureDetector.onTouchEvent(event);
+ return super.onTouchEvent(event);
+ }
+
+
+ private class ScaleGestureListener implements ScaleGestureDetector.OnScaleGestureListener{
+ @Override
+ public void onScaleEnd(ScaleGestureDetector detector) {
+ }
+
+ @Override
+ public boolean onScaleBegin(ScaleGestureDetector detector) {
+ return true;
+ }
+
+ @Override
+ public boolean onScale(ScaleGestureDetector detector) {
+ float newZoom = getZoom()*detector.getScaleFactor();
+ setZoom(newZoom);
+ setSelection(getSelectedItemPosition());
+ return true;
+ }
+ }
+
}
@@ -116,9 +116,14 @@ public View getView(int position, View convertView, ViewGroup parent) {
// Use this code if you want to load from resources
ImageView i = new ImageView(context);
- i.setImageBitmap(bitmaps.get(position));
- i.setLayoutParams(new CoverFlow.LayoutParams(130, 130));
+ Bitmap bitmap = bitmaps.get(position);
+ i.setImageBitmap(bitmap);
+ float resizeRatio = getResizeRatio(bitmap.getWidth(), bitmap.getHeight(), 130);
+ i.setLayoutParams(new CoverFlow.LayoutParams(
+ (int)(resizeRatio * bitmap.getWidth()), (int)(resizeRatio * bitmap.getHeight())
+ ));
i.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
+ i.setBackgroundColor(android.R.color.background_dark);
//i.setBackgroundResource(galleryItemBackground);
// Make sure we set anti-aliasing otherwise we get jaggies
@@ -128,6 +133,14 @@ public View getView(int position, View convertView, ViewGroup parent) {
// return mImages[position];
}
+
+ private float getResizeRatio(int width, int height, int maxTarget){
+ float wRatio = (float) maxTarget / width;
+ float hRatio = (float) maxTarget / height;
+ return wRatio < hRatio && wRatio < 1
+ ? wRatio
+ : hRatio;
+ }
/**
* Returns the size (0.0f to 1.0f) of the views depending on the 'offset' to
@@ -1,7 +1,9 @@
package com.kaulahcintaku.comicshelf;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
@@ -74,5 +76,33 @@ public void close(){
cursor.close();
return results;
}
+
+ public List<Item> getLastReads(List<String> lastReadPaths){
+ StringBuilder sql = new StringBuilder();
+ sql.append("select book_cate, book_path, book_cover from bookfolder where book_path in (");
+ if(lastReadPaths.size() > 0){
+ for(String string : lastReadPaths) {
+ sql.append("'");
+ sql.append(string);
+ sql.append("',");
+ }
+ sql.setLength(sql.length() - 1);
+ }
+ sql.append(")");
+ Cursor cursor = database.rawQuery(sql.toString(), null);
+ Map<String, Item> tempTable = new HashMap<String, Item>();
+ while(cursor.moveToNext()){
+ Item comic = new Item(cursor.getBlob(2), cursor.getString(0), cursor.getString(1), false);
+ comic.setDetail(cursor.getString(1));
+ tempTable.put(comic.getDetail(), comic);
+ }
+ cursor.close();
+ List<Item> results = new ArrayList<Item>();
+ for(String string : lastReadPaths) {
+ if(tempTable.containsKey(string))
+ results.add(tempTable.get(string));
+ }
+ return results;
+ }
}
@@ -0,0 +1,161 @@
+package com.kaulahcintaku.comicshelf;
+
+import java.io.DataOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import android.content.Context;
+import android.content.SharedPreferences;
+import android.util.Log;
+
+public class PerfectViewerPreferences {
+
+ private static final String HISTORY_FILE = "perfect_viewer_history";
+
+ public File getSharedPrefsFile(String name) {
+ //return new File("mnt/sdcard/Android/data/shared_prefs/", name + ".xml");
+ //return new File("/data/data/com.example.coverflowv2/shared_prefs/", name + ".xml");
+ return new File("/data/data/com.rookiestudio.perfectviewer/shared_prefs/", name + ".xml");
+ }
+
+ public SharedPreferences getSharedPreferences(String name) throws Exception{
+ SharedPreferences sp = null;
+ File file = getSharedPrefsFile(name);
+ if(!file.exists())
+ throw new RuntimeException("file: "+file.getAbsolutePath()+" doesn't exists");
+ if(!file.canRead() && !makeFileReadable(file))
+ throw new RuntimeException("file is not readable");
+ FileInputStream str = null;
+ str = new FileInputStream(file);
+ Map map = XmlUtilsReadMapXml(str);
+ str.close();
+ sp = newPreferences(file, Context.MODE_PRIVATE, map);
+ return sp;
+ }
+
+ private boolean makeFileReadable(File file) throws Exception{
+ return executeSuCommand("chmod 664 "+file.getAbsolutePath());
+ }
+
+ public static boolean executeSuCommand(String command) throws Exception{
+ Process process = Runtime.getRuntime().exec("su -c sh");
+ DataOutputStream os = new DataOutputStream(process.getOutputStream());
+ os.writeBytes(command); os.flush();
+ os.writeBytes("\n"); os.flush();
+ os.writeBytes("exit\n"); os.flush();
+ if(process.waitFor() != 0)
+ throw new RuntimeException(command+" returns "+process.exitValue());
+ return true;
+ }
+
+ private Map XmlUtilsReadMapXml(FileInputStream fileInputStream) throws Exception{
+ Class clazz = Class.forName("com.android.internal.util.XmlUtils");
+ Method method = clazz.getMethod("readMapXml", InputStream.class);
+ return (Map) method.invoke(null, fileInputStream);
+ }
+
+ private SharedPreferences newPreferences(File file, int mode, final Map map) throws Exception{
+ return new SharedPreferencesImpl(file, mode, map);
+ }
+
+ public List<String> getLastReads() throws Exception{
+ List<String> results = new ArrayList<String>();
+ SharedPreferences sp = getSharedPreferences(HISTORY_FILE);
+ for(int i = 0; i < 10; i++){
+ String value = sp.getString("HistoryFolder"+i, null);
+ if(value != null)
+ results.add(value);
+ }
+ return results;
+ }
+
+ private static final class SharedPreferencesImpl implements SharedPreferences {
+
+ private Map mMap;
+
+ SharedPreferencesImpl(
+ File file, int mode, Map initialContents) {
+ mMap = initialContents != null ? initialContents : new HashMap();
+ }
+
+ public boolean hasFileChanged() {
+ synchronized (this) {
+ return false;
+ }
+ }
+
+ public void replace(Map newContents) {
+ if (newContents != null) {
+ synchronized (this) {
+ mMap = newContents;
+ }
+ }
+ }
+
+ public void registerOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) {
+ throw new UnsupportedOperationException();
+ }
+
+ public void unregisterOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) {
+ throw new UnsupportedOperationException();
+ }
+
+ public Map<String, ?> getAll() {
+ synchronized(this) {
+ //noinspection unchecked
+ return new HashMap(mMap);
+ }
+ }
+
+ public String getString(String key, String defValue) {
+ synchronized (this) {
+ String v = (String)mMap.get(key);
+ return v != null ? v : defValue;
+ }
+ }
+
+ public int getInt(String key, int defValue) {
+ synchronized (this) {
+ Integer v = (Integer)mMap.get(key);
+ return v != null ? v : defValue;
+ }
+ }
+ public long getLong(String key, long defValue) {
+ synchronized (this) {
+ Long v = (Long) mMap.get(key);
+ return v != null ? v : defValue;
+ }
+ }
+ public float getFloat(String key, float defValue) {
+ synchronized (this) {
+ Float v = (Float)mMap.get(key);
+ return v != null ? v : defValue;
+ }
+ }
+ public boolean getBoolean(String key, boolean defValue) {
+ synchronized (this) {
+ Boolean v = (Boolean)mMap.get(key);
+ return v != null ? v : defValue;
+ }
+ }
+
+ public boolean contains(String key) {
+ synchronized (this) {
+ return mMap.containsKey(key);
+ }
+ }
+
+ @Override
+ public Editor edit() {
+ throw new UnsupportedOperationException();
+ }
+ }
+
+}
Oops, something went wrong.

0 comments on commit 5f51f60

Please sign in to comment.