diff --git a/AndroidManifest.xml b/AndroidManifest.xml index da29d269f..f1238025c 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -2,7 +2,7 @@ + android:versionName="0.4.5" > + android:versionName="0.4.5" > # Encoding is UTF-8 -1397228709198 627582 db0f122c99ef9f90dbab3fada6d191f2880cbb8e E:\code\SlidingMenu\library\libs\android-support-v4.jar +1389885111965 627582 db0f122c99ef9f90dbab3fada6d191f2880cbb8e E:\code\SlidingMenu\library\libs\android-support-v4.jar 1389885111965 627582 db0f122c99ef9f90dbab3fada6d191f2880cbb8e E:\Android\code\EhViewer\libs\android-support-v4.jar diff --git a/bin/resources.ap_ b/bin/resources.ap_ index ab8230e78..552351dfc 100644 Binary files a/bin/resources.ap_ and b/bin/resources.ap_ differ diff --git a/src/com/hippo/ehviewer/AppContext.java b/src/com/hippo/ehviewer/AppContext.java index d68159c66..01181f01c 100644 --- a/src/com/hippo/ehviewer/AppContext.java +++ b/src/com/hippo/ehviewer/AppContext.java @@ -10,8 +10,6 @@ import com.hippo.ehviewer.util.Config; import com.hippo.ehviewer.util.Crash; import com.hippo.ehviewer.util.Download; import com.hippo.ehviewer.util.EhClient; -import com.hippo.ehviewer.util.Favourite; -import com.hippo.ehviewer.util.Tag; import com.hippo.ehviewer.util.ThreadPool; import com.hippo.ehviewer.util.Ui; @@ -39,9 +37,9 @@ public class AppContext extends Application implements UncaughtExceptionHandler Config.init(this); Ui.init(this); Cache.init(this); - Favourite.init(this); + //Favourite.init(this); Crash.init(this); - Tag.init(this); + //Tag.init(this); BeautifyScreen.init(this); mEhClient = new EhClient(this); Download.init(this); @@ -61,6 +59,10 @@ public class AppContext extends Application implements UncaughtExceptionHandler return mNetworkThreadPool; } + public Data getData() { + return mData; + } + @Override public void uncaughtException(Thread thread, Throwable ex) { if (!handleException(ex) && mDefaultHandler != null) { diff --git a/src/com/hippo/ehviewer/Tag.java b/src/com/hippo/ehviewer/Tag.java index b462b43a7..d1dbdb8bb 100644 --- a/src/com/hippo/ehviewer/Tag.java +++ b/src/com/hippo/ehviewer/Tag.java @@ -8,6 +8,11 @@ public class Tag extends ListUrls { mName = name; } + public Tag(String name, ListUrls lus) { + this(name, lus.getType(), lus.getSearch()); + this.setAdvance(lus.getAdvanceType(), lus.getMinRating()); + } + public void setName(String name) { mName = name; } diff --git a/src/com/hippo/ehviewer/activity/DownloadActivity.java b/src/com/hippo/ehviewer/activity/DownloadActivity.java index 8235a87af..924d5fbf9 100644 --- a/src/com/hippo/ehviewer/activity/DownloadActivity.java +++ b/src/com/hippo/ehviewer/activity/DownloadActivity.java @@ -15,7 +15,6 @@ import com.hippo.ehviewer.util.Cache; import com.hippo.ehviewer.util.Config; import com.hippo.ehviewer.util.Download; import com.hippo.ehviewer.util.EhClient; -import com.hippo.ehviewer.util.Favourite; import com.hippo.ehviewer.util.Util; import com.hippo.ehviewer.view.AlertButton; import com.hippo.ehviewer.view.DownloadItemLayout; diff --git a/src/com/hippo/ehviewer/activity/FavouriteActivity.java b/src/com/hippo/ehviewer/activity/FavouriteActivity.java index 790cf9e62..0dc26ce0b 100644 --- a/src/com/hippo/ehviewer/activity/FavouriteActivity.java +++ b/src/com/hippo/ehviewer/activity/FavouriteActivity.java @@ -1,6 +1,7 @@ package com.hippo.ehviewer.activity; import java.util.ArrayList; +import java.util.List; import android.app.Activity; import android.app.AlertDialog; @@ -25,16 +26,17 @@ import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemLongClickListener; import android.widget.Toast; +import com.hippo.ehviewer.AppContext; import com.hippo.ehviewer.BeautifyScreen; import com.hippo.ehviewer.GalleryInfo; import com.hippo.ehviewer.ImageLoadManager; import com.hippo.ehviewer.R; +import com.hippo.ehviewer.data.Data; import com.hippo.ehviewer.service.DownloadService; import com.hippo.ehviewer.service.DownloadServiceConnection; import com.hippo.ehviewer.util.Cache; import com.hippo.ehviewer.util.Config; import com.hippo.ehviewer.util.EhClient; -import com.hippo.ehviewer.util.Favourite; import com.hippo.ehviewer.util.Ui; import com.hippo.ehviewer.view.AlertButton; import com.hippo.ehviewer.view.OlImageView; @@ -44,8 +46,11 @@ import com.hippo.ehviewer.widget.LoadImageView; public class FavouriteActivity extends Activity{ private static final String TAG = "FavouriteActivity"; + private AppContext mAppContext; + private Data mData; + private FlAdapter flAdapter; - private ArrayList mFavouriteLmd; + private List mFavouriteLmd; private int longClickItemIndex; private ImageLoadManager mImageLoadManager; @@ -63,7 +68,7 @@ public class FavouriteActivity extends Activity{ int position, long arg3) { switch (position) { case 0: // Remove favourite item - Favourite.remove(longClickItemIndex); + mData.deleteLocalFavourite(mFavouriteLmd.get(longClickItemIndex).gid); flAdapter.notifyDataSetChanged(); break; case 1: @@ -163,6 +168,9 @@ public class FavouriteActivity extends Activity{ super.onCreate(savedInstanceState); setContentView(R.layout.favourite); + mAppContext = (AppContext)getApplication(); + mData = mAppContext.getData(); + int screenOri = Config.getScreenOriMode(); if (screenOri != getRequestedOrientation()) setRequestedOrientation(screenOri); @@ -182,7 +190,7 @@ public class FavouriteActivity extends Activity{ BeautifyScreen.ColourfyScreen(this); } - mFavouriteLmd = Favourite.getFavouriteList(); + mFavouriteLmd = mData.getAllLocalFavourites(); ListView listView = (ListView)findViewById(R.id.favourite); flAdapter = new FlAdapter(); diff --git a/src/com/hippo/ehviewer/activity/MangaDetailActivity.java b/src/com/hippo/ehviewer/activity/MangaDetailActivity.java index b76c9ea46..38ef9f35b 100644 --- a/src/com/hippo/ehviewer/activity/MangaDetailActivity.java +++ b/src/com/hippo/ehviewer/activity/MangaDetailActivity.java @@ -9,12 +9,12 @@ import com.hippo.ehviewer.GalleryInfo; import com.hippo.ehviewer.ListUrls; import com.hippo.ehviewer.PageList; import com.hippo.ehviewer.R; +import com.hippo.ehviewer.data.Data; import com.hippo.ehviewer.service.DownloadService; import com.hippo.ehviewer.service.DownloadServiceConnection; import com.hippo.ehviewer.util.Cache; import com.hippo.ehviewer.util.Config; import com.hippo.ehviewer.util.EhClient; -import com.hippo.ehviewer.util.Favourite; import com.hippo.ehviewer.util.Ui; import com.hippo.ehviewer.util.Util; import com.hippo.ehviewer.view.OlImageView; @@ -65,6 +65,7 @@ public class MangaDetailActivity extends Activity { public static final String KEY_G_INFO = "gallery_info"; private AppContext mAppContext; + private Data mData; private EhClient mEhClient; private String url; @@ -450,6 +451,7 @@ public class MangaDetailActivity extends Activity { mAppContext = (AppContext)getApplication(); mEhClient = mAppContext.getEhClient(); + mData = mAppContext.getData(); int screenOri = Config.getScreenOriMode(); if (screenOri != getRequestedOrientation()) @@ -645,7 +647,7 @@ public class MangaDetailActivity extends Activity { finish(); return true; case R.id.action_favourite: - Favourite.push(mangaDetail); + mData.addLocalFavourite(mangaDetail); Toast.makeText(MangaDetailActivity.this, getString(R.string.toast_add_favourite), Toast.LENGTH_SHORT).show(); diff --git a/src/com/hippo/ehviewer/activity/MangaListActivity.java b/src/com/hippo/ehviewer/activity/MangaListActivity.java index c36f13515..62035d0c3 100644 --- a/src/com/hippo/ehviewer/activity/MangaListActivity.java +++ b/src/com/hippo/ehviewer/activity/MangaListActivity.java @@ -2,6 +2,7 @@ package com.hippo.ehviewer.activity; import java.net.MalformedURLException; import java.util.ArrayList; +import java.util.List; import com.hippo.ehviewer.AppContext; import com.hippo.ehviewer.BeautifyScreen; @@ -9,16 +10,16 @@ import com.hippo.ehviewer.GalleryInfo; import com.hippo.ehviewer.ImageLoadManager; import com.hippo.ehviewer.ListUrls; import com.hippo.ehviewer.R; +import com.hippo.ehviewer.Tag; import com.hippo.ehviewer.UpdateHelper; +import com.hippo.ehviewer.data.Data; import com.hippo.ehviewer.network.Downloader; import com.hippo.ehviewer.service.DownloadService; import com.hippo.ehviewer.service.DownloadServiceConnection; import com.hippo.ehviewer.util.Cache; import com.hippo.ehviewer.util.Config; import com.hippo.ehviewer.util.EhClient; -import com.hippo.ehviewer.util.Favourite; import com.hippo.ehviewer.util.Log; -import com.hippo.ehviewer.util.Tag; import com.hippo.ehviewer.util.Ui; import com.hippo.ehviewer.util.Util; import com.hippo.ehviewer.view.AlertButton; @@ -111,6 +112,7 @@ public class MangaListActivity extends SlidingActivity { private ImageLoadManager mImageLoadManager; + private Data mData; private int longClickItemIndex; @@ -237,11 +239,10 @@ public class MangaListActivity extends SlidingActivity { @Override public void onSetVaildName(String newName) { ((AlertButton)v).dialog.dismiss(); - if (Tag.add(newName, getLus(filterDialog))) { - listMenuTitle.add(newName); - tagsAdapter.addId(newName); - tagsAdapter.notifyDataSetChanged(); - } + mData.addTag(new Tag(newName, getLus(filterDialog))); + listMenuTitle.add(newName); + tagsAdapter.addId(newName); + tagsAdapter.notifyDataSetChanged(); } }) .show(); @@ -361,7 +362,7 @@ public class MangaListActivity extends SlidingActivity { switch (position) { case 0: // Add favourite item lmd = lmdArray.get(longClickItemIndex); - Favourite.push(lmd); + mData.addLocalFavourite(lmd); Toast.makeText(MangaListActivity.this, getString(R.string.toast_add_favourite), Toast.LENGTH_SHORT).show(); @@ -502,7 +503,7 @@ public class MangaListActivity extends SlidingActivity { private AlertDialog createModifyTagDialog(final int position) { LayoutInflater inflater = this.getLayoutInflater(); final View view = inflater.inflate(R.layout.filter, null); - ListUrls listUrls = Tag.get(position - mStableItemCount); + ListUrls listUrls = mData.getTag(position - mStableItemCount); setFilterView(view, listUrls); final View advance = view.findViewById(R.id.filter_advance); CheckBox cb = (CheckBox)view.findViewById(R.id.checkbox_advance); @@ -528,13 +529,13 @@ public class MangaListActivity extends SlidingActivity { ListUrls listUrls = getLus(view); if (newTagName != null) { tagsAdapter.set(listMenuTitle.get(position), newTagName); - Tag.set(position - mStableItemCount, newTagName, listUrls); + mData.setTag(position - mStableItemCount, new Tag(newTagName, listUrls)); listMenuTitle.set(position, newTagName); tagsAdapter.notifyDataSetChanged(); newTagName = null; } else - Tag.set(position - mStableItemCount, listUrls); + mData.setTag(position - mStableItemCount, new Tag(listMenuTitle.get(position), listUrls)); } }).setNegativeButton(android.R.string.cancel, new View.OnClickListener() { @Override @@ -1060,6 +1061,7 @@ public class MangaListActivity extends SlidingActivity { }); mAppContext = (AppContext)getApplication(); + mData = mAppContext.getData(); mEhClient = mAppContext.getEhClient(); int screenOri = Config.getScreenOriMode(); @@ -1123,14 +1125,15 @@ public class MangaListActivity extends SlidingActivity { mStableItemCount = menuTitles.length; for (int i = 0; i < menuTitles.length; i++) listMenuTitle.add(menuTitles[i]); - ArrayList keys = Tag.getKeyList(); + + List keys = mData.getAllTagNames(); + for (int i = 0; i < keys.size(); i++) listMenuTitle.add(keys.get(i)); tagsAdapter = new TagsAdapter(this, R.layout.menu_item, listMenuTitle); listMenu.setAdapter(tagsAdapter); listMenu.setChoiceMode(ListView.CHOICE_MODE_SINGLE); listMenu.setStableItemCount(mStableItemCount); - listMenu.setItemList(listMenuTitle); listMenu.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView arg0, View arg1, @@ -1151,7 +1154,7 @@ public class MangaListActivity extends SlidingActivity { startActivity(intent); showContent(); } else if (position >= mStableItemCount){ - ListUrls listUrls = Tag.get(listMenuTitle.get(position)); + ListUrls listUrls = mData.getTag(position - mStableItemCount); if (listUrls != null && refresh(listUrls, listMenuTitle.get(position))) showContent(); } @@ -1163,6 +1166,28 @@ public class MangaListActivity extends SlidingActivity { createModifyTagDialog(position).show(); } }); + listMenu.setOnSwapListener(new TagListView.OnSwapListener() { + @Override + public void onSwap(int positionOne, int positionTwo) { + String temp = listMenuTitle.get(positionOne); + listMenuTitle.set(positionOne, listMenuTitle.get(positionTwo)); + listMenuTitle.set(positionTwo, temp); + tagsAdapter.notifyDataSetChanged(); + + mData.swapTag(positionOne - mStableItemCount, positionTwo - mStableItemCount); + } + }); + listMenu.setOnDeleteListener(new TagListView.OnDeleteListener() { + @Override + public void onDelete(int position) { + String removeItem = listMenuTitle.remove(position); + tagsAdapter.remove(removeItem); + tagsAdapter.notifyDataSetChanged(); + + mData.deleteTag(position - mStableItemCount); + } + }); + // is Exhentai final String[] isExhentaiListTitle = getResources().getStringArray(R.array.is_exhentai); diff --git a/src/com/hippo/ehviewer/data/Data.java b/src/com/hippo/ehviewer/data/Data.java index e12fe4175..6ccd044aa 100644 --- a/src/com/hippo/ehviewer/data/Data.java +++ b/src/com/hippo/ehviewer/data/Data.java @@ -1,8 +1,11 @@ package com.hippo.ehviewer.data; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import com.hippo.ehviewer.GalleryInfo; import com.hippo.ehviewer.ListUrls; import com.hippo.ehviewer.Tag; import com.hippo.ehviewer.util.Util; @@ -13,6 +16,7 @@ import android.database.Cursor; import android.database.DatabaseUtils; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; +import android.util.Log; /** * @author Hippo @@ -20,6 +24,7 @@ import android.database.sqlite.SQLiteOpenHelper; */ public class Data { + private static final String TAG = "Data"; private static final int mVersion = 1; private static final String DB_NAME = "data"; @@ -35,7 +40,7 @@ public class Data { private static final String COLUMN_POSTED = "posted"; private static final String COLUMN_CATEGORY = "category"; private static final String COLUMN_THUMB = "thumb"; - private static final String COLUMN_UPLODER = "uploder"; + private static final String COLUMN_UPLOADER = "uploader"; private static final String COLUMN_RATING = "rating"; private static final String COLUMN_REFERENCE = "reference"; @@ -45,8 +50,12 @@ public class Data { private static final String COLUMN_ADVANCE = "advance"; private static final String COLUMN_MIN_RATING = "min_rating"; + private Map mGallerys; + private long tagRowNum; - private List mTags = new ArrayList(); + private List mTags; + + private List mLocalFavourites; private Context mContext; @@ -58,7 +67,10 @@ public class Data { mDBHelper = new DBHelper(mContext); mDatabase = mDBHelper.getWritableDatabase(); + mGallerys = new HashMap(); + getTags(); + getLocalFavourites(); } @Override @@ -66,8 +78,201 @@ public class Data { mDBHelper.close(); } - /****** TAG ******/ - private synchronized List getTags() { + /****** gallery ******/ + public GalleryInfo getGallery(String gid) { + GalleryInfo galleryInfo = mGallerys.get(gid); + if (galleryInfo == null) { + Cursor cursor = mDatabase.rawQuery("select * from " + TABLE_GALLERY + " where " + COLUMN_GID + "=?", + new String[]{gid}); + if (cursor.moveToFirst()) { + galleryInfo = new GalleryInfo(); + galleryInfo.gid = gid; + galleryInfo.token = cursor.getString(1); + galleryInfo.title = cursor.getString(2); + galleryInfo.posted = cursor.getString(3); + galleryInfo.category = cursor.getInt(4); + galleryInfo.thumb = cursor.getString(5); + galleryInfo.uploader = cursor.getString(6); + galleryInfo.rating = cursor.getString(7); + + // add to map + mGallerys.put(gid, galleryInfo); + } + } + return galleryInfo; + } + + @SuppressWarnings("unused") + private synchronized boolean containsGallery(String gid) { + boolean re = false; + Cursor cursor = mDatabase.rawQuery("select * from " + + TABLE_GALLERY + " where " + COLUMN_GID + "=?", + new String[]{gid}); + if (cursor.moveToFirst()) + re = true; + cursor.close(); + return re; + } + + /** + * If do not contain the gallery return -1; + * @param gid + * @return + */ + private synchronized int getGalleryReference(String gid) { + int reference = -1; + Cursor cursor = mDatabase.rawQuery("select " + COLUMN_REFERENCE + " from " + + TABLE_GALLERY + " where " + COLUMN_GID + "=?", + new String[]{gid}); + if (cursor.moveToFirst()) { + reference = cursor.getInt(0); + } + cursor.close(); + return reference; + } + + /** + * + * If exits same gid, add the reference by 1. + * If exits no same gid, add it to table reference is 1 + * + * @param galleryInfo + * @param isUpdate + */ + private void addGallery(GalleryInfo galleryInfo, boolean isUpdate) { + boolean isInsert; + String gid = galleryInfo.gid; + // add to map + mGallerys.put(gid, galleryInfo); + + // add to sql or update and add reference by 1 + int reference; + if ((reference = getGalleryReference(gid)) != -1) { + isInsert = false; + if (!isUpdate) + reference++; + } + else { + isInsert = true; + reference = 1; + } + + ContentValues values = new ContentValues(); + values.put(COLUMN_GID, gid); + values.put(COLUMN_TOKEN, galleryInfo.token); + values.put(COLUMN_TITLE, galleryInfo.title); + values.put(COLUMN_POSTED, galleryInfo.posted); + values.put(COLUMN_CATEGORY, galleryInfo.category); + values.put(COLUMN_THUMB, galleryInfo.thumb); + values.put(COLUMN_UPLOADER, galleryInfo.uploader); + values.put(COLUMN_RATING, galleryInfo.rating); + values.put(COLUMN_REFERENCE, reference); + + if (isInsert) { // Set + mDatabase.insert(TABLE_GALLERY, null, values); + } else { // Update + mDatabase.update(TABLE_GALLERY, values, COLUMN_GID + "=?", new String[]{gid}); + } + } + + /** + * decrease reference by 1, only delete when reference is 0 + * @param gid + */ + private void deleteGallery(String gid) { + int reference = getGalleryReference(gid); + if (reference == -1) { + Log.w(TAG, "Can't get reference when deleteGallery, gid is " + gid); + return; + } + reference--; + if (reference == 0) { // delete gallery + mDatabase.delete(TABLE_GALLERY, COLUMN_GID + "=?", new String[]{gid}); + } else { // update reference + ContentValues values = new ContentValues(); + values.put(COLUMN_REFERENCE, reference); + mDatabase.update(TABLE_GALLERY, values, COLUMN_GID + "=?", new String[]{gid}); + } + } + + /****** local favourite ******/ + private synchronized void getLocalFavourites() { + mLocalFavourites = new ArrayList(); + Cursor cursor = mDatabase.rawQuery("select * from " + + TABLE_LOCAL_FAVOURITE, null); + + List invaildGids = new ArrayList(); + + if (cursor.moveToFirst()) { + while (!cursor.isAfterLast()) { + String gid = cursor.getString(0); + GalleryInfo galleryInfo = getGallery(gid); + if (galleryInfo == null) { + invaildGids.add(gid); + } else { + mLocalFavourites.add(galleryInfo); + } + cursor.moveToNext(); + } + } + cursor.close(); + + // remove invail gid + for (String invaildGid : invaildGids) { + mDatabase.delete(TABLE_LOCAL_FAVOURITE, COLUMN_GID + "=?", + new String[]{String.valueOf(invaildGid)}); + } + } + + public synchronized boolean containsLocalFavourite(String gid) { + boolean re = false; + Cursor cursor = mDatabase.rawQuery("select * from " + + TABLE_LOCAL_FAVOURITE + " where " + COLUMN_GID + "=?", + new String[]{gid}); + if (cursor.moveToFirst()) + re = true; + cursor.close(); + return re; + } + + /** + * galleryInfo can't be null + * @param galleryInfo + */ + public synchronized void addLocalFavourite(GalleryInfo galleryInfo) { + // add to sql + ContentValues values = new ContentValues(); + values.put(COLUMN_GID, galleryInfo.gid); + boolean isUpdate = mDatabase.insert( + TABLE_LOCAL_FAVOURITE, null, values) == -1; + addGallery(galleryInfo, isUpdate); + + // add to list + if (!isUpdate) + mLocalFavourites.add(galleryInfo); + } + + public synchronized void deleteLocalFavourite(String gid) { + // delete from list + for (GalleryInfo galleryInfo : mLocalFavourites) { + if (galleryInfo.gid.equals(gid)) { + mLocalFavourites.remove(galleryInfo); + break; + } + } + + // delete from sql + mDatabase.delete(TABLE_LOCAL_FAVOURITE, COLUMN_GID + "=?", new String[]{gid}); + deleteGallery(gid); + } + + public synchronized List getAllLocalFavourites() { + return mLocalFavourites; + } + + + /****** tag ******/ + private synchronized void getTags() { mTags = new ArrayList(); tagRowNum = 0; Cursor cursor = mDatabase.rawQuery("select * from " + TABLE_TAG @@ -90,13 +295,13 @@ public class Data { tagRowNum++; } } - return mTags; + cursor.close(); } - public synchronized boolean addTag(String name, Tag tag) { + public synchronized boolean addTag(Tag tag) { ContentValues values = new ContentValues(); values.put(COLUMN_ID, tagRowNum); - values.put(COLUMN_NAME, name); + values.put(COLUMN_NAME, tag.getName()); values.put(COLUMN_CATEGORY, tag.getType()); values.put(COLUMN_SEARCH, tag.getSearch()); values.put(COLUMN_ADVANCE, tag.getAdvanceType()); @@ -111,15 +316,65 @@ public class Data { return ok; } - public synchronized void deleteTag(int id) { + public synchronized boolean deleteTag(int id) { int deleteNum = mDatabase.delete(TABLE_TAG, COLUMN_ID + "=?", new String[]{String.valueOf(id)}); + if (deleteNum > 1) + Log.e(TAG, "WTF? more than one id is " + id); + + if (deleteNum == 0) + return false; + if (id < 0 || id >= mTags.size()) { + Log.e(TAG, "id is out of bounds, id is " + id + ", mTags.size() is " + mTags.size()); + return false; + } + + // Update sql + ContentValues values = new ContentValues(); + for (int i = id + 1; i < tagRowNum; i++) { + values.put(COLUMN_ID, i - 1); + mDatabase.update(TABLE_TAG, values, COLUMN_ID + "=?", new String[]{String.valueOf(i)}); + } tagRowNum -= deleteNum; + + // Update list + mTags.remove(id); + + return true; } - public List getAllTags() { + public synchronized List getAllTags() { return mTags; } + public synchronized Tag getTag(int location) { + return mTags.get(location); + } + + public synchronized List getAllTagNames() { + List names = new ArrayList(); + for (Tag tag : mTags) + names.add(tag.getName()); + return names; + } + + public synchronized void setTag(int location, Tag tag) { + if (location == tagRowNum) { + addTag(tag); + } else { + // Update list + mTags.set(location, tag); + + // Update sql + ContentValues values = new ContentValues(); + values.put(COLUMN_NAME, tag.getName()); + values.put(COLUMN_CATEGORY, tag.getType()); + values.put(COLUMN_SEARCH, tag.getSearch()); + values.put(COLUMN_ADVANCE, tag.getAdvanceType()); + values.put(COLUMN_MIN_RATING, tag.getMinRating()); + mDatabase.update(TABLE_TAG, values, COLUMN_ID + "=?", new String[]{String.valueOf(location)}); + } + } + public synchronized boolean swapTag(int indexOne, int indexTwo) { if (indexOne < 0 || indexOne > tagRowNum || indexTwo < 0 || indexTwo > tagRowNum) @@ -139,12 +394,10 @@ public class Data { values.put(COLUMN_ID, indexOne); mDatabase.update(TABLE_TAG, values, COLUMN_ID + "=?", new String[]{String.valueOf(indexTwo)}); values.put(COLUMN_ID, indexTwo); - mDatabase.update(TABLE_TAG, values, COLUMN_ID + "=?", new String[]{String.valueOf(indexOne)}); + mDatabase.update(TABLE_TAG, values, COLUMN_ID + "=?", new String[]{String.valueOf(-1)}); return true; } - - public class DBHelper extends SQLiteOpenHelper { public DBHelper(Context context) { @@ -153,6 +406,7 @@ public class Data { @Override public void onCreate(SQLiteDatabase db) { + // gallery String CreateGallery = "create table " + TABLE_GALLERY + "(" + "gid text primary key," + "token text," @@ -160,21 +414,24 @@ public class Data { + "posted text," + "category integer," + "thumb text," - + "uploder text," + + "uploader text," + "rating text," + "reference integer);"; db.execSQL(CreateGallery); + // read String CreateRead = "create table " + TABLE_READ + "(" + "gid text primary key);"; db.execSQL(CreateRead); + // local favourite String CreateLocalFavourite = "create table " + TABLE_LOCAL_FAVOURITE + "(" + "gid text primary key);"; db.execSQL(CreateLocalFavourite); + // tag String CreateTag = "create table " + TABLE_TAG + "(" + "id integer primary key," diff --git a/src/com/hippo/ehviewer/util/Favourite.java b/src/com/hippo/ehviewer/util/Favourite.java deleted file mode 100644 index f6c7861ff..000000000 --- a/src/com/hippo/ehviewer/util/Favourite.java +++ /dev/null @@ -1,212 +0,0 @@ -package com.hippo.ehviewer.util; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; -import java.io.OptionalDataException; -import java.util.ArrayList; - -import com.hippo.ehviewer.GalleryInfo; - -import android.content.Context; -import android.content.SharedPreferences; - -public class Favourite { - //private static final String TAG = "Favourite"; - - private static final String SHA_PRE_NAME = "favourite"; - private static final String LIST_ORDER = "list_order"; - - private static Context mContext; - private static SharedPreferences mFavouritePre; - //private static boolean isLru = false; // TODO float the last read favourite item - private static ArrayList mListOrder = null; - private static ArrayList mFavouriteLmd = null; - - private static boolean mInit = false; - - /** - * Init Favourite - * - * @param context Application context - */ - public static void init(Context context) { - if (mInit) - return; - mInit = true; - - mContext = context; - mFavouritePre = mContext.getSharedPreferences(SHA_PRE_NAME, 0); - - getFavourite(); - } - - /** - * Is init - * @return True if init - */ - public static boolean isInit() { - return mInit; - } - - private static void getFavourite() { - mListOrder = new ArrayList(); - mFavouriteLmd = new ArrayList(); - String[] allOrder = Util.getStrings(mFavouritePre, LIST_ORDER); - - if (allOrder == null) - return; - - boolean errorFlag = false; - for (String item : allOrder) { - mListOrder.add(item); - String strLmd = mFavouritePre.getString(item, null); - if (strLmd == null) { - mListOrder.remove(strLmd); - errorFlag = true; - continue; - } - mFavouriteLmd.add(decodeLmd(strLmd)); - } - if (errorFlag) - Util.putStrings(mFavouritePre, LIST_ORDER, mListOrder); - } - - /** - * Add a new favourite item to the top of favourite list - * - * @param lmd - * @return True if push successfully - */ - public static boolean push(GalleryInfo lmd) { - if (!mInit) { - throw new IllegalStateException("Please init Favourite first."); - } - // Check contain this item or not - int oldIndex; - if ((oldIndex = mListOrder.indexOf(lmd.gid)) != -1) { - mListOrder.remove(oldIndex); - mListOrder.add(0, lmd.gid); - mFavouriteLmd.remove(oldIndex); - mFavouriteLmd.add(0, lmd); - } else { - mListOrder.add(0, lmd.gid); - mFavouriteLmd.add(0, lmd); - } - - Util.putStrings(mFavouritePre, LIST_ORDER, mListOrder); - mFavouritePre.edit().putString(lmd.gid, encodeLmd(lmd)).apply(); - - return true; - } - - /** - * Remove favourite item - * - * @param index - * @return True if remove successfully - */ - public static boolean remove(int index) { - mFavouriteLmd.remove(index); - String key = mListOrder.remove(index); - Util.putStrings(mFavouritePre, LIST_ORDER, mListOrder); - mFavouritePre.edit().remove(key).apply(); - return true; - } - - /** - * Please do not change it - * - * @return - */ - public static ArrayList getFavouriteList() { - return mFavouriteLmd; - } - - /** - * Top a favourite item - * - * @param index - */ - public static void top(int index) { - // TODO - } - - /** - * Update favourite item data - */ - public static void updateData() { - // TODO - } - - protected static String encodeLmd(GalleryInfo lmd) { - ByteArrayOutputStream os = new ByteArrayOutputStream(); - try { - ObjectOutputStream outputStream = new ObjectOutputStream(os); - writeObject(lmd, outputStream); - } catch (Exception e) { - e.printStackTrace(); - return null; - } finally { - try { - os.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - return Util.byteArrayToHexString(os.toByteArray()); - } - - protected static GalleryInfo decodeLmd(String lmdStr) { - byte[] bytes = Util.hexStringToByteArray(lmdStr); - ByteArrayInputStream is = new ByteArrayInputStream(bytes); - GalleryInfo lmd = null; - try { - ObjectInputStream ois = new ObjectInputStream(is); - lmd = readObject(ois); - } catch (Exception e) { - e.printStackTrace(); - } finally { - try { - is.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - return lmd; - } - - public static void writeObject(GalleryInfo lmd, ObjectOutputStream out) throws IOException { - out.writeObject(lmd.gid); - out.writeObject(lmd.token); - out.writeObject(lmd.title); - out.writeObject((Integer)lmd.category); - out.writeObject(lmd.thumb); - out.writeObject(lmd.uploader); - out.writeObject(lmd.posted); - out.writeObject(lmd.rating); - } - - public static GalleryInfo readObject(ObjectInputStream in) { - GalleryInfo lmd = new GalleryInfo(); - try { - lmd.gid = (String)in.readObject(); - lmd.token = (String)in.readObject(); - lmd.title = (String)in.readObject(); - lmd.category = (Integer)in.readObject(); - lmd.thumb = (String)in.readObject(); - lmd.uploader = (String)in.readObject(); - lmd.posted = (String)in.readObject(); - lmd.rating = (String)in.readObject(); - } catch (OptionalDataException e) { - e.printStackTrace(); - } catch (ClassNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - return lmd; - } -} diff --git a/src/com/hippo/ehviewer/util/Tag.java b/src/com/hippo/ehviewer/util/Tag.java deleted file mode 100644 index 8a43641a9..000000000 --- a/src/com/hippo/ehviewer/util/Tag.java +++ /dev/null @@ -1,279 +0,0 @@ -package com.hippo.ehviewer.util; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; -import java.io.OptionalDataException; -import java.util.ArrayList; - -import com.hippo.ehviewer.ListUrls; - -import android.content.Context; -import android.content.SharedPreferences; - -public class Tag { - - private static final String SHA_PRE_NAME = "tag"; - private static final String LIST_ORDER = "list_order"; - - private static Context mContext; - private static SharedPreferences mTagPre; - private static ArrayList mListOrder = new ArrayList(); - private static ArrayList mTags = new ArrayList(); - - private static boolean mInit = false; - /** - * Init Tag - * - * @param context Application context - */ - public static void init(Context context) { - if (mInit) - return; - mInit = true; - - mContext = context; - mTagPre = mContext.getSharedPreferences(SHA_PRE_NAME, 0); - - getTag(); - } - - /** - * Is init - * @return True if init - */ - public static boolean isInit() { - return mInit; - } - - private static void getTag() { - String[] allOrder = Util.getStrings(mTagPre, LIST_ORDER); - - if (allOrder == null) - return; - - boolean errorFlag = false; - for (String item : allOrder) { - mListOrder.add(item); - String strTag = mTagPre.getString(item, null); - if (strTag == null) { - mListOrder.remove(strTag); - errorFlag = true; - continue; - } - mTags.add(decode(strTag)); - } - if (errorFlag) - Util.putStrings(mTagPre, LIST_ORDER, mListOrder); - } - - /** - * Add a new tag item to the end of tag list - * - * @param key - * @param tag - * @return - */ - public static boolean add(String key, ListUrls tag) { - // Check contain this item or not - if (mListOrder.indexOf(key) == -1) { - mListOrder.add(key); - mTags.add(tag); - Util.putStrings(mTagPre, LIST_ORDER, mListOrder); - mTagPre.edit().putString(key, encode(tag)).apply(); - return true; - } else - return false; - } - - /** - * Swap tag list order - * - * @param oldIndex - * @param newIndex - * @return - */ - public static boolean swap(int indexOne, int indexTwo) { - if (indexOne < 0 || indexOne > mListOrder.size() - || indexTwo < 0 || indexTwo > mListOrder.size()) - return false; - - if (indexOne == indexTwo) - return true; - - String temp1 = mListOrder.get(indexOne); - mListOrder.set(indexOne, mListOrder.get(indexTwo)); - mListOrder.set(indexTwo, temp1); - Util.putStrings(mTagPre, LIST_ORDER, mListOrder); - - ListUrls temp2 = mTags.get(indexOne); - mTags.set(indexOne, mTags.get(indexTwo)); - mTags.set(indexTwo, temp2); - return true; - } - - public static boolean contains(String key) { - return mListOrder.contains(key); - } - - /** - * Remove Tag item - * - * @param index - * @return True if remove successfully - */ - public static boolean remove(String key) { - int index = mListOrder.indexOf(key); - if (index != -1) { - mListOrder.remove(index); - mTags.remove(index); - mTagPre.edit().remove(key).apply(); - Util.putStrings(mTagPre, LIST_ORDER, mListOrder); - return true; - } else - return false; - } - - /** - * Remove Tag item - * - * @param index - * @return True if remove successfully - */ - public static boolean remove(int index) { - mTags.remove(index); - String key = mListOrder.remove(index); - mTagPre.edit().remove(key).apply(); - Util.putStrings(mTagPre, LIST_ORDER, mListOrder); - return true; - } - - public static ListUrls get(String key) { - int index = mListOrder.indexOf(key); - if (index == -1) - return null; - return mTags.get(index); - } - - public static ListUrls get(int position) { - return mTags.get(position); - } - - public static String getKey(int position) { - return mListOrder.get(position); - } - - public static void set(int position, String newKey, ListUrls listUrls) { - String oldKey = mListOrder.set(position, newKey); - mTags.set(position, listUrls); - - Util.putStrings(mTagPre, LIST_ORDER, mListOrder); - mTagPre.edit().remove(oldKey).putString(newKey, encode(listUrls)).apply(); - } - - public static void set(int position, ListUrls listUrls) { - mTags.set(position, listUrls); - mTagPre.edit().putString(mListOrder.get(position), encode(listUrls)).apply(); - } - - /** - * You must not modify what you get - * - * @return - */ - public static ArrayList getTagList() { - return mTags; - } - - /** - * You must not modify what you get - * - * @return - */ - public static ArrayList getKeyList() { - return mListOrder; - } - - public static void set(String oldKey, String newKey, ListUrls lus) { - int index = mListOrder.indexOf(oldKey); - if (index == -1) { - mListOrder.set(index, newKey); - mTags.set(index, lus); - } - } - - protected static String encode(ListUrls tag) { - ByteArrayOutputStream os = new ByteArrayOutputStream(); - try { - ObjectOutputStream outputStream = new ObjectOutputStream(os); - writeObject(tag, outputStream); - } catch (Exception e) { - e.printStackTrace(); - return null; - } finally { - try { - os.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - return Util.byteArrayToHexString(os.toByteArray()); - } - - protected static ListUrls decode(String strTag) { - byte[] bytes = Util.hexStringToByteArray(strTag); - ByteArrayInputStream is = new ByteArrayInputStream(bytes); - ListUrls tag = null; - try { - ObjectInputStream ois = new ObjectInputStream(is); - tag = readObject(ois); - } catch (Exception e) { - e.printStackTrace(); - } finally { - try { - is.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - return tag; - } - - public static void writeObject(ListUrls tag, ObjectOutputStream out) throws IOException { - out.writeObject((Integer)tag.getType()); - out.writeObject(tag.getSearch()); - out.writeObject((Boolean)tag.isAdvance()); - out.writeObject((Integer)tag.getAdvanceType()); - out.writeObject((Boolean)tag.isMinRating()); - out.writeObject((Integer)tag.getMinRating()); - } - - public static ListUrls readObject(ObjectInputStream in) { - ListUrls tag = null; - try { - int type = (Integer)in.readObject(); - String search = (String)in.readObject(); - tag = new ListUrls(type, search); - - boolean isAdvance = (Boolean)in.readObject(); - int advanceType = (Integer)in.readObject(); - boolean isMinRating = (Boolean)in.readObject(); - int minRating = (Integer)in.readObject(); - if (isAdvance) { - if (isMinRating) - tag.setAdvance(advanceType, minRating); - else - tag.setAdvance(advanceType); - } - } catch (OptionalDataException e) { - e.printStackTrace(); - } catch (ClassNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - return tag; - } -} diff --git a/src/com/hippo/ehviewer/view/TagListView.java b/src/com/hippo/ehviewer/view/TagListView.java index 428f28416..b59179997 100644 --- a/src/com/hippo/ehviewer/view/TagListView.java +++ b/src/com/hippo/ehviewer/view/TagListView.java @@ -25,11 +25,9 @@ import android.animation.ValueAnimator; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; -import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.graphics.drawable.BitmapDrawable; -import android.provider.Contacts.Intents.UI; import android.util.AttributeSet; import android.util.DisplayMetrics; import android.view.MotionEvent; @@ -37,16 +35,11 @@ import android.view.View; import android.view.ViewTreeObserver; import android.widget.AbsListView; import android.widget.AdapterView; -import android.widget.BaseAdapter; import android.widget.ListView; import java.util.ArrayList; import java.util.HashMap; -import com.hippo.ehviewer.util.Log; -import com.hippo.ehviewer.util.Tag; -import com.hippo.ehviewer.util.Ui; - /** * The dynamic listview is an extension of listview that supports cell dragging * and swapping. @@ -74,10 +67,9 @@ public class TagListView extends ListView { private final int SMOOTH_SCROLL_AMOUNT_AT_EDGE = 15; private final int MOVE_DURATION = 150; - private final int LINE_THICKNESS = Ui.dp2pix(3); private final int SENSITY = 5; - public ArrayList mItemList; + //public ArrayList mItemList; private int mLastEventX = -1; private int mLastEventY = -1; @@ -113,8 +105,10 @@ public class TagListView extends ListView { private boolean mIsWaitingForScrollFinish = false; private int mScrollState = OnScrollListener.SCROLL_STATE_IDLE; - private OnModifyListener listener = null; - private OnMoveLister movelistener = null; + private OnModifyListener mModifylistener = null; + private OnMoveLister mMovelistener = null; + private OnDeleteListener mOnDeleteListener = null; + private OnSwapListener mOnSwapListener = null; public TagListView(Context context) { super(context); @@ -148,8 +142,8 @@ public class TagListView extends ListView { if (pos < mStableItemCount) return true; - if (movelistener != null) - movelistener.onMoveStart(); + if (mMovelistener != null) + mMovelistener.onMoveStart(); mTotalOffset = 0; @@ -303,7 +297,7 @@ public class TagListView extends ListView { } public void setOnModifyListener(OnModifyListener listener) { - this.listener = listener; + this.mModifylistener = listener; } public interface OnMoveLister { @@ -312,7 +306,23 @@ public class TagListView extends ListView { } public void setOnMoveLister(OnMoveLister listener) { - this.movelistener = listener; + this.mMovelistener = listener; + } + + public interface OnDeleteListener { + public void onDelete(int position); + } + + public void setOnDeleteListener(OnDeleteListener listener) { + mOnDeleteListener = listener; + } + + public interface OnSwapListener { + public void onSwap(int positionOne, int positionTwo); + } + + public void setOnSwapListener(OnSwapListener listener) { + mOnSwapListener = listener; } /** @@ -380,13 +390,13 @@ public class TagListView extends ListView { } break; case MotionEvent.ACTION_UP: - if (movelistener != null) - movelistener.onMoveOver(); + if (mMovelistener != null) + mMovelistener.onMoveOver(); touchEventsEnded(); break; case MotionEvent.ACTION_CANCEL: - if (movelistener != null) - movelistener.onMoveOver(); + if (mMovelistener != null) + mMovelistener.onMoveOver(); touchEventsCancelled(); break; case MotionEvent.ACTION_POINTER_UP: @@ -398,8 +408,8 @@ public class TagListView extends ListView { MotionEvent.ACTION_POINTER_INDEX_SHIFT; final int pointerId = event.getPointerId(pointerIndex); if (pointerId == mActivePointerId) { - if (movelistener != null) - movelistener.onMoveOver(); + if (mMovelistener != null) + mMovelistener.onMoveOver(); touchEventsEnded(); } break; @@ -441,10 +451,9 @@ public class TagListView extends ListView { return; } - swapElements(mItemList, originalItem, getPositionForView(switchView)); - Tag.swap(originalItem - mStableItemCount, getPositionForView(switchView) - mStableItemCount); - - ((BaseAdapter)getAdapter()).notifyDataSetChanged(); + if (mOnSwapListener != null) { + mOnSwapListener.onSwap(originalItem, getPositionForView(switchView)); + } mDownY = mLastEventY; @@ -515,8 +524,8 @@ public class TagListView extends ListView { if (mIsModify) { mIsModify = false; - if (listener != null) { - listener.onModify(getPositionForID(mMobileItemId)); + if (mModifylistener != null) { + mModifylistener.onModify(getPositionForID(mMobileItemId)); } } @@ -578,9 +587,8 @@ public class TagListView extends ListView { } // Delete the item from the adapter and tag int position = getPositionForView(viewToRemove); - String key = mAdapter.getItem(position); - mAdapter.remove(key); - Tag.remove(key); + if (mOnDeleteListener != null) + mOnDeleteListener.onDelete(position); final ViewTreeObserver observer = getViewTreeObserver(); observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { @@ -728,10 +736,6 @@ public class TagListView extends ListView { return false; } - public void setItemList(ArrayList itemList) { - mItemList = itemList; - } - /** * This scroll listener is added to the listview in order to handle cell swapping * when the cell is either at the top or bottom edge of the listview. If the hover diff --git a/src/com/hippo/ehviewer/view/TagsAdapter.java b/src/com/hippo/ehviewer/view/TagsAdapter.java index 50169da3d..427b72159 100644 --- a/src/com/hippo/ehviewer/view/TagsAdapter.java +++ b/src/com/hippo/ehviewer/view/TagsAdapter.java @@ -17,20 +17,11 @@ package com.hippo.ehviewer.view; import android.content.Context; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; import android.widget.ArrayAdapter; -import android.widget.BaseAdapter; -import android.widget.TextView; -import java.util.ArrayList; import java.util.HashMap; import java.util.List; -import com.hippo.ehviewer.R; -import com.hippo.ehviewer.util.Tag; - public class TagsAdapter extends ArrayAdapter { final int INVALID_ID = -1;