diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java b/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java index c09f937ee..789905c90 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java @@ -139,6 +139,7 @@ import fr.gouv.etalab.mastodon.sqlite.AccountDAO; import fr.gouv.etalab.mastodon.sqlite.InstancesDAO; import fr.gouv.etalab.mastodon.sqlite.SearchDAO; import fr.gouv.etalab.mastodon.sqlite.Sqlite; +import fr.gouv.etalab.mastodon.sqlite.StatusCacheDAO; import static fr.gouv.etalab.mastodon.asynctasks.ManageFiltersAsyncTask.action.GET_ALL_FILTER; import static fr.gouv.etalab.mastodon.helper.Helper.ADD_USER_INTENT; @@ -1207,48 +1208,63 @@ public abstract class BaseMainActivity extends BaseActivity startActivity(intent); return true; case R.id.action_cache: - AlertDialog.Builder builder = new AlertDialog.Builder(BaseMainActivity.this, style); - builder.setTitle(R.string.cache_title); - long sizeCache = Helper.cacheSize(getCacheDir()); - float cacheSize = 0; - if( sizeCache > 0 ) { - cacheSize = (float) sizeCache / 1000000.0f; - } - final float finalCacheSize = cacheSize; - builder.setMessage(getString(R.string.cache_message, String.format("%s %s", String.format(Locale.getDefault(), "%.2f", cacheSize), getString(R.string.cache_units)))) - .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int which) { - // continue with delete - AsyncTask.execute(new Runnable() { - @Override - public void run() { - try { - String path = getCacheDir().getPath(); - File dir = new File(path); - if (dir.isDirectory()) { - Helper.deleteDir(dir); + + new AsyncTask() { + private float cacheSize; + @Override + protected Void doInBackground(Void... params) { + long sizeCache = Helper.cacheSize(getCacheDir().getParentFile()); + cacheSize = 0; + if( sizeCache > 0 ) { + cacheSize = (float) sizeCache / 1000000.0f; + } + return null; + } + @Override + protected void onPostExecute(Void result){ + AlertDialog.Builder builder = new AlertDialog.Builder(BaseMainActivity.this, style); + builder.setTitle(R.string.cache_title); + + final float finalCacheSize = cacheSize; + builder.setMessage(getString(R.string.cache_message, String.format("%s %s", String.format(Locale.getDefault(), "%.2f", cacheSize), getString(R.string.cache_units)))) + .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + // continue with delete + AsyncTask.execute(new Runnable() { + @Override + public void run() { + try { + String path = getCacheDir().getParentFile().getPath(); + File dir = new File(path); + if (dir.isDirectory()) { + Helper.deleteDir(dir); + } + } catch (Exception ignored) {} + new StatusCacheDAO(BaseMainActivity.this, db).removeAllStatus(StatusCacheDAO.STATUS_CACHE); } - } catch (Exception ignored) {} + }); + Toast.makeText(BaseMainActivity.this, getString(R.string.toast_cache_clear,String.format("%s %s", String.format(Locale.getDefault(), "%.2f", finalCacheSize), getString(R.string.cache_units))), Toast.LENGTH_LONG).show(); + dialog.dismiss(); } - }); - Toast.makeText(BaseMainActivity.this, getString(R.string.toast_cache_clear,String.format("%s %s", String.format(Locale.getDefault(), "%.2f", finalCacheSize), getString(R.string.cache_units))), Toast.LENGTH_LONG).show(); - dialog.dismiss(); - } - }) - .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int which) { - dialog.dismiss(); - } - }) - .setIcon(android.R.drawable.ic_dialog_alert) - .show(); + }) + .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + dialog.dismiss(); + } + }) + .setIcon(android.R.drawable.ic_dialog_alert) + .show(); + } + }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + + return true; case R.id.action_size: final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); int textSize = sharedpreferences.getInt(Helper.SET_TEXT_SIZE,110); int iconSize = sharedpreferences.getInt(Helper.SET_ICON_SIZE,130); - builder = new AlertDialog.Builder(BaseMainActivity.this, style); + AlertDialog.Builder builder = new AlertDialog.Builder(BaseMainActivity.this, style); builder.setTitle(R.string.text_size); @SuppressLint("InflateParams") View popup_quick_settings = getLayoutInflater().inflate( R.layout.popup_text_size, null ); diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java b/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java index e7659e1c4..b888e898c 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java @@ -17,6 +17,7 @@ package fr.gouv.etalab.mastodon.client; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.util.Log; import org.json.JSONArray; import org.json.JSONException; @@ -2474,39 +2475,47 @@ public class API { try { card.setAuthor_name(resobj.get("author_name").toString()); }catch (Exception e){ + e.printStackTrace(); card.setAuthor_name(null); } try { card.setAuthor_url(resobj.get("author_url").toString()); }catch (Exception e){ + e.printStackTrace(); card.setAuthor_url(null); } try { card.setEmbed_url(resobj.get("embed_url").toString()); }catch (Exception e){ + e.printStackTrace(); card.setEmbed_url(null); } try { card.setProvider_name(resobj.get("provider_name").toString()); }catch (Exception e){ + e.printStackTrace(); card.setProvider_name(null); } try { card.setProvider_url(resobj.get("provider_url").toString()); }catch (Exception e){ + e.printStackTrace(); card.setProvider_url(null); } try { card.setHeight(Integer.parseInt(resobj.get("height").toString())); }catch (Exception e){ + e.printStackTrace(); card.setHeight(0); } try { card.setWidth(Integer.parseInt(resobj.get("width").toString())); }catch (Exception e){ + e.printStackTrace(); card.setWidth(0); } } catch (JSONException e) { + e.printStackTrace(); card = null; } return card; @@ -2876,8 +2885,9 @@ public class API { } } try { + status.setCard(parseCardResponse(resobj.getJSONObject("card"))); - }catch (Exception e){status.setCard(null);} + }catch (Exception e){status.setCard(null); Log.v(Helper.TAG,"resobj: " + resobj);e.printStackTrace();} status.setMedia_attachments(attachments); diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/Card.java b/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/Card.java index 9590acdab..f33631f4a 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/Card.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/Card.java @@ -43,6 +43,17 @@ public class Card implements Parcelable { public Card(){} + public static final Creator CREATOR = new Creator() { + @Override + public Card createFromParcel(Parcel in) { + return new Card(in); + } + + @Override + public Card[] newArray(int size) { + return new Card[size]; + } + }; protected Card(Parcel in) { url = in.readString(); @@ -60,17 +71,24 @@ public class Card implements Parcelable { width = in.readInt(); } - public static final Creator CREATOR = new Creator() { - @Override - public Card createFromParcel(Parcel in) { - return new Card(in); - } + @Override + public void writeToParcel(Parcel dest, int flags) { + dest.writeString(url); + dest.writeString(title); + dest.writeString(description); + dest.writeString(image); + dest.writeString(type); + dest.writeString(html); + dest.writeString(author_name); + dest.writeString(author_url); + dest.writeString(embed_url); + dest.writeString(provider_name); + dest.writeString(provider_url); + dest.writeInt(height); + dest.writeInt(width); + } + - @Override - public Card[] newArray(int size) { - return new Card[size]; - } - }; public String getUrl() { return url; @@ -182,20 +200,5 @@ public class Card implements Parcelable { return 0; } - @Override - public void writeToParcel(Parcel dest, int flags) { - dest.writeString(url); - dest.writeString(title); - dest.writeString(description); - dest.writeString(image); - dest.writeString(type); - dest.writeString(html); - dest.writeString(author_name); - dest.writeString(author_url); - dest.writeString(embed_url); - dest.writeString(provider_name); - dest.writeString(provider_url); - dest.writeInt(height); - dest.writeInt(width); - } + } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java b/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java index d6e91ffaa..b7a6326b6 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java @@ -163,6 +163,7 @@ import fr.gouv.etalab.mastodon.client.API; import fr.gouv.etalab.mastodon.client.Entities.Account; import fr.gouv.etalab.mastodon.client.Entities.Application; import fr.gouv.etalab.mastodon.client.Entities.Attachment; +import fr.gouv.etalab.mastodon.client.Entities.Card; import fr.gouv.etalab.mastodon.client.Entities.Emojis; import fr.gouv.etalab.mastodon.client.Entities.Filters; import fr.gouv.etalab.mastodon.client.Entities.Mention; @@ -1570,9 +1571,11 @@ public class Helper { if (dir != null && dir.isDirectory()) { String[] children = dir.list(); for (String aChildren : children) { - boolean success = deleteDir(new File(dir, aChildren)); - if (!success) { - return false; + if (!aChildren.equals("databases") && !aChildren.equals("shared_prefs")) { + boolean success = deleteDir(new File(dir, aChildren)); + if (!success) { + return false; + } } } return dir.delete(); @@ -1913,6 +1916,30 @@ public class Helper { return gson.toJson(status); } + /** + * Unserialized a Card + * @param serializedCard String serialized card + * @return Card + */ + public static Card restoreCardFromString(String serializedCard){ + Gson gson = new Gson(); + try { + return gson.fromJson(serializedCard, Card.class); + }catch (Exception e){ + return null; + } + } + + /** + * Serialized a Card class + * @param card Card to serialize + * @return String serialized Status + */ + public static String cardToStringStorage(Card card){ + Gson gson = new Gson(); + return gson.toJson(card); + } + /** * Unserialized a Status * @param serializedStatus String serialized status @@ -1927,7 +1954,6 @@ public class Helper { } } - /** * Serialized a List * @param list List to serialize diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/sqlite/Sqlite.java b/app/src/main/java/fr/gouv/etalab/mastodon/sqlite/Sqlite.java index 6918f2e2a..a1be7f2bb 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/sqlite/Sqlite.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/sqlite/Sqlite.java @@ -26,7 +26,7 @@ import android.database.sqlite.SQLiteOpenHelper; public class Sqlite extends SQLiteOpenHelper { - public static final int DB_VERSION = 15; + public static final int DB_VERSION = 16; public static final String DB_NAME = "mastodon_etalab_db"; public static SQLiteDatabase db; private static Sqlite sInstance; @@ -146,7 +146,7 @@ public class Sqlite extends SQLiteOpenHelper { static final String COL_LANGUAGE = "LANGUAGE"; static final String COL_PINNED = "PINNED"; static final String COL_DATE_BACKUP = "DATE_BACKUP"; - + static final String COL_CARD = "CARD"; private final String CREATE_TABLE_STATUSES_CACHE = "CREATE TABLE " + TABLE_STATUSES_CACHE + " (" + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " @@ -156,7 +156,7 @@ public class Sqlite extends SQLiteOpenHelper { + COL_REBLOG + " TEXT, " + COL_CONTENT + " TEXT NOT NULL, " + COL_CREATED_AT + " TEXT NOT NULL, " + COL_EMOJIS + " TEXT, " + COL_REBLOGS_COUNT + " INTEGER NOT NULL, " + COL_FAVOURITES_COUNT + " INTEGER NOT NULL, " + COL_REBLOGGED + " INTEGER, " + COL_FAVOURITED + " INTEGER, " + COL_MUTED + " INTEGER, " + COL_SENSITIVE + " INTEGER, " - + COL_SPOILER_TEXT + " TEXT, " + COL_VISIBILITY + " TEXT NOT NULL, " + COL_MEDIA_ATTACHMENTS + " TEXT," + + COL_SPOILER_TEXT + " TEXT, " + COL_VISIBILITY + " TEXT NOT NULL, " + COL_MEDIA_ATTACHMENTS + " TEXT," + COL_CARD + " TEXT," + COL_MENTIONS + " TEXT, " + COL_TAGS + " TEXT, " + COL_APPLICATION + " TEXT," + COL_LANGUAGE + " TEXT," + COL_PINNED + " INTEGER)"; @@ -247,6 +247,9 @@ public class Sqlite extends SQLiteOpenHelper { db.execSQL("ALTER TABLE " + TABLE_INSTANCES + " ADD COLUMN "+ COL_INSTANCE_TYPE + " TEXT"); case 14: db.execSQL(CREATE_TABLE_PEERTUBE_FAVOURITES); + case 15: + if( oldVersion > 8) + db.execSQL("ALTER TABLE " + TABLE_STATUSES_CACHE + " ADD COLUMN "+ COL_CARD + " TEXT"); default: break; } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/sqlite/StatusCacheDAO.java b/app/src/main/java/fr/gouv/etalab/mastodon/sqlite/StatusCacheDAO.java index 7fff4fce7..51bb64157 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/sqlite/StatusCacheDAO.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/sqlite/StatusCacheDAO.java @@ -117,6 +117,7 @@ public class StatusCacheDAO { values.put(Sqlite.COL_URI, status.getUri()); values.put(Sqlite.COL_URL, status.getUrl()); values.put(Sqlite.COL_ACCOUNT, Helper.accountToStringStorage(status.getAccount())); + values.put(Sqlite.COL_CARD, Helper.cardToStringStorage(status.getCard())); values.put(Sqlite.COL_IN_REPLY_TO_ID, status.getIn_reply_to_id()); values.put(Sqlite.COL_IN_REPLY_TO_ACCOUNT_ID, status.getIn_reply_to_account_id()); values.put(Sqlite.COL_REBLOG, status.getReblog()!=null?Helper.statusToStringStorage(status.getReblog()):null); @@ -447,6 +448,7 @@ public class StatusCacheDAO { status.setUri(c.getString(c.getColumnIndex(Sqlite.COL_URI))); status.setUrl(c.getString(c.getColumnIndex(Sqlite.COL_URL))); status.setAccount(Helper.restoreAccountFromString(c.getString(c.getColumnIndex(Sqlite.COL_ACCOUNT)))); + status.setCard(Helper.restoreCardFromString(c.getString(c.getColumnIndex(Sqlite.COL_CARD)))); status.setIn_reply_to_id(c.getString(c.getColumnIndex(Sqlite.COL_IN_REPLY_TO_ID))); status.setIn_reply_to_account_id(c.getString(c.getColumnIndex(Sqlite.COL_IN_REPLY_TO_ACCOUNT_ID))); status.setReblog(Helper.restoreStatusFromString(c.getString(c.getColumnIndex(Sqlite.COL_REBLOG))));