diff --git a/app/src/main/java/app/fedilab/fedilabtube/AccountActivity.java b/app/src/main/java/app/fedilab/fedilabtube/AccountActivity.java index 8f94d47..fc7b223 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/AccountActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/AccountActivity.java @@ -15,7 +15,6 @@ package app.fedilab.fedilabtube; * see . */ import android.content.Intent; -import android.content.SharedPreferences; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.text.SpannableString; @@ -75,9 +74,8 @@ public class AccountActivity extends AppCompatActivity { Spanned.SPAN_INCLUSIVE_EXCLUSIVE); - SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE); SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); - String token = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null); + String token = Helper.getToken(AccountActivity.this); TextView instanceView = findViewById(R.id.instance); Account account = new AccountDAO(AccountActivity.this, db).getAccountByToken(token); diff --git a/app/src/main/java/app/fedilab/fedilabtube/MainActivity.java b/app/src/main/java/app/fedilab/fedilabtube/MainActivity.java index 1686e50..9d26ebf 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/MainActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/MainActivity.java @@ -559,7 +559,7 @@ public class MainActivity extends AppCompatActivity implements ChromeCastsListen private void refreshToken() { new Thread(() -> { final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); - String tokenStr = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null); + String tokenStr = Helper.getToken(MainActivity.this); String instance = HelperInstance.getLiveInstance(MainActivity.this); SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); String instanceShar = sharedpreferences.getString(Helper.PREF_INSTANCE, null); diff --git a/app/src/main/java/app/fedilab/fedilabtube/PeertubeActivity.java b/app/src/main/java/app/fedilab/fedilabtube/PeertubeActivity.java index 9e84c86..80653b8 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/PeertubeActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/PeertubeActivity.java @@ -309,7 +309,7 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd max_id = "0"; SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE); - String token = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null); + String token = Helper.getToken(PeertubeActivity.this); if (Helper.isLoggedIn(PeertubeActivity.this) && !sepiaSearch) { Account account = new AccountDAO(PeertubeActivity.this, db).getAccountByToken(token); Helper.loadGiF(PeertubeActivity.this, account.getAvatar() != null ? account.getAvatar().getPath() : null, binding.myPp); diff --git a/app/src/main/java/app/fedilab/fedilabtube/PeertubeUploadActivity.java b/app/src/main/java/app/fedilab/fedilabtube/PeertubeUploadActivity.java index 1b0945d..41c84ec 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/PeertubeUploadActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/PeertubeUploadActivity.java @@ -321,7 +321,7 @@ public class PeertubeUploadActivity extends AppCompatActivity { } try { SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); - String token = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null); + String token = Helper.getToken(PeertubeUploadActivity.this); new MultipartUploadRequest(PeertubeUploadActivity.this, "https://" + HelperInstance.getLiveInstance(PeertubeUploadActivity.this) + "/api/v1/videos/upload") .setMethod("POST") diff --git a/app/src/main/java/app/fedilab/fedilabtube/client/MastodonService.java b/app/src/main/java/app/fedilab/fedilabtube/client/MastodonService.java index 7becc1c..72a611f 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/client/MastodonService.java +++ b/app/src/main/java/app/fedilab/fedilabtube/client/MastodonService.java @@ -15,7 +15,7 @@ package app.fedilab.fedilabtube.client; * see . */ -import app.fedilab.fedilabtube.client.data.AccountData; +import app.fedilab.fedilabtube.client.entities.MastodonAccount; import app.fedilab.fedilabtube.client.entities.Oauth; import app.fedilab.fedilabtube.client.entities.Token; import retrofit2.Call; @@ -45,6 +45,6 @@ interface MastodonService { @Field("code") String code); @GET("accounts/verify_credentials") - Call verifyCredentials(@Header("Authorization") String credentials); + Call verifyCredentials(@Header("Authorization") String credentials); } diff --git a/app/src/main/java/app/fedilab/fedilabtube/client/RetrofitMastodonAPI.java b/app/src/main/java/app/fedilab/fedilabtube/client/RetrofitMastodonAPI.java index a7d7c97..306317e 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/client/RetrofitMastodonAPI.java +++ b/app/src/main/java/app/fedilab/fedilabtube/client/RetrofitMastodonAPI.java @@ -14,13 +14,13 @@ import java.net.URLDecoder; import app.fedilab.fedilabtube.MainActivity; import app.fedilab.fedilabtube.R; -import app.fedilab.fedilabtube.client.data.AccountData; import app.fedilab.fedilabtube.client.entities.Error; +import app.fedilab.fedilabtube.client.entities.MastodonAccount; import app.fedilab.fedilabtube.client.entities.Oauth; import app.fedilab.fedilabtube.client.entities.OauthParams; import app.fedilab.fedilabtube.client.entities.Token; import app.fedilab.fedilabtube.helper.Helper; -import app.fedilab.fedilabtube.sqlite.AccountDAO; +import app.fedilab.fedilabtube.sqlite.MastodonAccountDAO; import app.fedilab.fedilabtube.sqlite.Sqlite; import retrofit2.Call; import retrofit2.Response; @@ -44,7 +44,7 @@ public class RetrofitMastodonAPI { public void updateCredential(Activity activity, String client_id, String client_secret, String refresh_token, String software) { new Thread(() -> { - AccountData.Account account; + MastodonAccount.Account account; try { account = new RetrofitMastodonAPI(activity, instance, token).verifyCredentials(); } catch (Error error) { @@ -63,17 +63,19 @@ public class RetrofitMastodonAPI { account.setClient_secret(client_secret); account.setRefresh_token(refresh_token); account.setHost(instance); + account.setSoftware(software); SQLiteDatabase db = Sqlite.getInstance(activity.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); - boolean userExists = new AccountDAO(activity, db).userExist(account); + boolean userExists = new MastodonAccountDAO(activity, db).userExist(account); SharedPreferences.Editor editor = sharedpreferences.edit(); editor.putString(Helper.PREF_KEY_ID, account.getId()); editor.putString(Helper.PREF_KEY_NAME, account.getUsername()); + editor.putString(Helper.PREF_SOFTWARE, software); editor.apply(); if (userExists) - new AccountDAO(activity, db).updateAccountCredential(account); + new MastodonAccountDAO(activity, db).updateAccountCredential(account); else { if (account.getUsername() != null && account.getCreatedAt() != null) - new AccountDAO(activity, db).insertAccount(account, software); + new MastodonAccountDAO(activity, db).insertAccount(account); } Handler mainHandler = new Handler(Looper.getMainLooper()); Runnable myRunnable = () -> { @@ -91,9 +93,8 @@ public class RetrofitMastodonAPI { .baseUrl(finalUrl) .addConverterFactory(GsonConverterFactory.create()) .build(); - SharedPreferences sharedpreferences = _context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); if (token == null) { - token = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null); + token = Helper.getToken(_context); } return retrofit.create(MastodonService.class); } @@ -123,12 +124,12 @@ public class RetrofitMastodonAPI { * Verifiy credential of the authenticated user *synchronously* * @return Account */ - public AccountData.Account verifyCredentials() throws Error { + public MastodonAccount.Account verifyCredentials() throws Error { MastodonService mastodonService = init(); - Call accountCall = mastodonService.verifyCredentials("Bearer " + token); + Call accountCall = mastodonService.verifyCredentials("Bearer " + token); APIResponse apiResponse = new APIResponse(); try { - Response response = accountCall.execute(); + Response response = accountCall.execute(); if (response.isSuccessful() && response.body() != null) { return response.body(); } else { diff --git a/app/src/main/java/app/fedilab/fedilabtube/client/RetrofitPeertubeAPI.java b/app/src/main/java/app/fedilab/fedilabtube/client/RetrofitPeertubeAPI.java index 1aad1d3..dddda2e 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/client/RetrofitPeertubeAPI.java +++ b/app/src/main/java/app/fedilab/fedilabtube/client/RetrofitPeertubeAPI.java @@ -201,7 +201,7 @@ public class RetrofitPeertubeAPI { .build(); SharedPreferences sharedpreferences = _context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); if (token == null) { - token = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null); + token = Helper.getToken(_context); } selection = sharedpreferences.getStringSet(_context.getString(R.string.set_video_language_choice), null); return retrofit.create(PeertubeService.class); diff --git a/app/src/main/java/app/fedilab/fedilabtube/client/entities/MastodonAccount.java b/app/src/main/java/app/fedilab/fedilabtube/client/entities/MastodonAccount.java new file mode 100644 index 0000000..32762e0 --- /dev/null +++ b/app/src/main/java/app/fedilab/fedilabtube/client/entities/MastodonAccount.java @@ -0,0 +1,240 @@ +package app.fedilab.fedilabtube.client.entities; +/* Copyright 2020 Thomas Schneider + * + * This file is a part of TubeLab + * + * This program 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. + * + * TubeLab 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 TubeLab; if not, + * see . */ + +import android.os.Parcel; +import android.os.Parcelable; + +import com.google.gson.annotations.SerializedName; + +import java.util.Date; + + +@SuppressWarnings({"unused", "RedundantSuppression"}) +public class MastodonAccount { + + + public static class Account implements Parcelable { + public static final Creator CREATOR = new Creator() { + @Override + public Account createFromParcel(Parcel source) { + return new Account(source); + } + + @Override + public Account[] newArray(int size) { + return new Account[size]; + } + }; + @SerializedName("avatar") + private String avatar; + @SerializedName("createdAt") + private Date createdAt; + @SerializedName("note") + private String description; + @SerializedName("display_name") + private String displayName; + @SerializedName("followers_count") + private int followersCount; + @SerializedName("following_count") + private int followingCount; + @SerializedName("id") + private String id; + @SerializedName("username") + private String username; + @SerializedName("updatedAt") + private Date updatedAt; + @SerializedName("url") + private String url; + private String token; + private String client_id; + private String client_secret; + private String refresh_token; + private String software; + private String host; + + public Account() { + } + + protected Account(Parcel in) { + this.avatar = in.readString(); + long tmpCreatedAt = in.readLong(); + this.createdAt = tmpCreatedAt == -1 ? null : new Date(tmpCreatedAt); + this.description = in.readString(); + this.displayName = in.readString(); + this.followersCount = in.readInt(); + this.followingCount = in.readInt(); + this.id = in.readString(); + this.username = in.readString(); + long tmpUpdatedAt = in.readLong(); + this.updatedAt = tmpUpdatedAt == -1 ? null : new Date(tmpUpdatedAt); + this.url = in.readString(); + } + + public String getAvatar() { + return avatar; + } + + public void setAvatar(String avatar) { + this.avatar = avatar; + } + + public Date getCreatedAt() { + return createdAt; + } + + public void setCreatedAt(Date createdAt) { + this.createdAt = createdAt; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getDisplayName() { + return displayName; + } + + public void setDisplayName(String displayName) { + this.displayName = displayName; + } + + public int getFollowersCount() { + return followersCount; + } + + public void setFollowersCount(int followersCount) { + this.followersCount = followersCount; + } + + public int getFollowingCount() { + return followingCount; + } + + public void setFollowingCount(int followingCount) { + this.followingCount = followingCount; + } + + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + + public Date getUpdatedAt() { + return updatedAt; + } + + public void setUpdatedAt(Date updatedAt) { + this.updatedAt = updatedAt; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public String getAcct() { + return username + "@" + host; + } + + public String getToken() { + return token; + } + + public void setToken(String token) { + this.token = token; + } + + public String getClient_id() { + return client_id; + } + + public void setClient_id(String client_id) { + this.client_id = client_id; + } + + public String getClient_secret() { + return client_secret; + } + + public void setClient_secret(String client_secret) { + this.client_secret = client_secret; + } + + public String getRefresh_token() { + return refresh_token; + } + + public void setRefresh_token(String refresh_token) { + this.refresh_token = refresh_token; + } + + + public String getSoftware() { + return software; + } + + public void setSoftware(String software) { + this.software = software; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getHost() { + return host; + } + + public void setHost(String host) { + this.host = host; + } + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + dest.writeString(this.avatar); + dest.writeLong(this.createdAt != null ? this.createdAt.getTime() : -1); + dest.writeString(this.description); + dest.writeString(this.displayName); + dest.writeInt(this.followersCount); + dest.writeInt(this.followingCount); + dest.writeString(this.id); + dest.writeString(this.username); + dest.writeLong(this.updatedAt != null ? this.updatedAt.getTime() : -1); + dest.writeString(this.url); + } + } +} diff --git a/app/src/main/java/app/fedilab/fedilabtube/helper/Helper.java b/app/src/main/java/app/fedilab/fedilabtube/helper/Helper.java index 47f6331..dff1f43 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/helper/Helper.java +++ b/app/src/main/java/app/fedilab/fedilabtube/helper/Helper.java @@ -77,6 +77,7 @@ import es.dmoral.toasty.Toasty; import jp.wasabeef.glide.transformations.BlurTransformation; import static android.content.Context.DOWNLOAD_SERVICE; +import static android.content.Context.MODE_PRIVATE; public class Helper { @@ -342,7 +343,7 @@ public class Helper { } else { webView = rootView.findViewById(webviewId); } - final SharedPreferences sharedpreferences = activity.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + final SharedPreferences sharedpreferences = activity.getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setUseWideViewPort(true); @@ -421,7 +422,7 @@ public class Helper { * @param account Account */ public static void logoutCurrentUser(Activity activity, Account account) { - SharedPreferences sharedpreferences = activity.getSharedPreferences(APP_PREFS, Context.MODE_PRIVATE); + SharedPreferences sharedpreferences = activity.getSharedPreferences(APP_PREFS, MODE_PRIVATE); //Current user SQLiteDatabase db = Sqlite.getInstance(activity.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); @@ -459,7 +460,7 @@ public class Helper { * @param activity Activity */ public static void logoutNoRemoval(Activity activity) { - SharedPreferences sharedpreferences = activity.getSharedPreferences(APP_PREFS, Context.MODE_PRIVATE); + SharedPreferences sharedpreferences = activity.getSharedPreferences(APP_PREFS, MODE_PRIVATE); SharedPreferences.Editor editor = sharedpreferences.edit(); editor.putString(PREF_KEY_OAUTH_TOKEN, null); editor.putString(CLIENT_ID, null); @@ -507,7 +508,7 @@ public class Helper { * @return MainActivity.TypeOfConnection */ private static MainActivity.TypeOfConnection isLoggedInType(Context context) { - SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE); String prefKeyOauthTokenT = sharedpreferences.getString(PREF_KEY_OAUTH_TOKEN, null); String prefSoftware = sharedpreferences.getString(PREF_SOFTWARE, null); if (prefKeyOauthTokenT != null && prefSoftware == null) { @@ -519,6 +520,14 @@ public class Helper { } } + public static String getToken(Context context) { + if (isLoggedInType(context) == MainActivity.TypeOfConnection.NORMAL) { + SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE); + return sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null); + } else { + return null; + } + } /** * Converts dp to pixel @@ -544,7 +553,7 @@ public class Helper { public static boolean isOwner(Context context, Account account) { - SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE); String userName = sharedpreferences.getString(Helper.PREF_KEY_NAME, ""); String instance = sharedpreferences.getString(Helper.PREF_INSTANCE, ""); if (instance != null && userName != null) { @@ -555,7 +564,7 @@ public class Helper { } public static boolean isVideoOwner(Context context, VideoData.Video video) { - SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE); String userName = sharedpreferences.getString(Helper.PREF_KEY_NAME, ""); String instance = sharedpreferences.getString(Helper.PREF_INSTANCE, ""); if (video == null) { @@ -582,7 +591,7 @@ public class Helper { if (files == null || files.size() == 0) { return null; } - SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE); int video_quality = sharedpreferences.getInt(Helper.SET_QUALITY_MODE, Helper.QUALITY_HIGH); if (video_quality == QUALITY_HIGH) { return files.get(0); diff --git a/app/src/main/java/app/fedilab/fedilabtube/sqlite/MastodonAccountDAO.java b/app/src/main/java/app/fedilab/fedilabtube/sqlite/MastodonAccountDAO.java new file mode 100644 index 0000000..3ae544c --- /dev/null +++ b/app/src/main/java/app/fedilab/fedilabtube/sqlite/MastodonAccountDAO.java @@ -0,0 +1,369 @@ +package app.fedilab.fedilabtube.sqlite; +/* Copyright 2020 Thomas Schneider + * + * This file is a part of TubeLab + * + * This program 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. + * + * TubeLab 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 TubeLab; if not, + * see . */ + +import android.content.ContentValues; +import android.content.Context; +import android.content.SharedPreferences; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import app.fedilab.fedilabtube.client.entities.MastodonAccount.Account; +import app.fedilab.fedilabtube.client.entities.Token; +import app.fedilab.fedilabtube.helper.Helper; +import app.fedilab.fedilabtube.helper.HelperInstance; + + +@SuppressWarnings("UnusedReturnValue") +public class MastodonAccountDAO { + + private final SQLiteDatabase db; + public Context context; + + + public MastodonAccountDAO(Context context, SQLiteDatabase db) { + //Creation of the DB with tables + this.context = context; + this.db = db; + } + + + /** + * Insert an Account in database + * + * @param account Account + * @return boolean + */ + public boolean insertAccount(Account account) { + ContentValues values = new ContentValues(); + if (account.getCreatedAt() == null) + account.setCreatedAt(new Date()); + if (account.getDescription() == null) + account.setDescription(""); + values.put(Sqlite.COL_USER_ID, account.getId()); + values.put(Sqlite.COL_USERNAME, account.getUsername()); + values.put(Sqlite.COL_ACCT, account.getUsername() + "@" + account.getHost()); + values.put(Sqlite.COL_DISPLAYED_NAME, account.getDisplayName() != null ? account.getDisplayName() : account.getUsername()); + values.put(Sqlite.COL_FOLLOWERS_COUNT, account.getFollowersCount()); + values.put(Sqlite.COL_FOLLOWING_COUNT, account.getFollowingCount()); + values.put(Sqlite.COL_NOTE, account.getDescription()); + values.put(Sqlite.COL_URL, account.getUrl()); + values.put(Sqlite.COL_AVATAR, account.getAvatar()); + values.put(Sqlite.COL_CREATED_AT, Helper.dateToString(account.getCreatedAt())); + values.put(Sqlite.COL_INSTANCE, account.getHost()); + values.put(Sqlite.COL_LOCKED, false); + values.put(Sqlite.COL_STATUSES_COUNT, 0); + values.put(Sqlite.COL_URL, ""); + values.put(Sqlite.COL_AVATAR_STATIC, ""); + values.put(Sqlite.COL_HEADER, ""); + values.put(Sqlite.COL_HEADER_STATIC, ""); + if (account.getSoftware() != null && account.getSoftware().toUpperCase().trim().compareTo("PEERTUBE") != 0) { + values.put(Sqlite.COL_SOFTWARE, account.getSoftware().toUpperCase().trim()); + } + if (account.getClient_id() != null && account.getClient_secret() != null) { + values.put(Sqlite.COL_CLIENT_ID, account.getClient_id()); + values.put(Sqlite.COL_CLIENT_SECRET, account.getClient_secret()); + } + if (account.getRefresh_token() != null) { + values.put(Sqlite.COL_REFRESH_TOKEN, account.getRefresh_token()); + } + if (account.getToken() != null) + values.put(Sqlite.COL_OAUTHTOKEN, account.getToken()); + + //Inserts account + try { + db.insertOrThrow(Sqlite.TABLE_USER_ACCOUNT, null, values); + + } catch (Exception e) { + e.printStackTrace(); + return false; + } + return true; + } + + /** + * Update an Account in database + * + * @param account Account + * @return boolean + */ + public int updateAccount(Account account) { + ContentValues values = new ContentValues(); + if (account.getCreatedAt() == null) + account.setCreatedAt(new Date()); + if (account.getDescription() == null) + account.setDescription(""); + values.put(Sqlite.COL_USER_ID, account.getId()); + values.put(Sqlite.COL_USERNAME, account.getUsername()); + values.put(Sqlite.COL_ACCT, account.getUsername() + "@" + account.getHost()); + values.put(Sqlite.COL_DISPLAYED_NAME, account.getDisplayName()); + values.put(Sqlite.COL_FOLLOWERS_COUNT, account.getFollowersCount()); + values.put(Sqlite.COL_FOLLOWING_COUNT, account.getFollowingCount()); + values.put(Sqlite.COL_NOTE, account.getDescription()); + values.put(Sqlite.COL_URL, account.getUrl()); + values.put(Sqlite.COL_AVATAR, account.getAvatar()); + values.put(Sqlite.COL_CREATED_AT, Helper.dateToString(account.getCreatedAt())); + + try { + return db.update(Sqlite.TABLE_USER_ACCOUNT, + values, Sqlite.COL_USERNAME + " = ? AND " + Sqlite.COL_INSTANCE + " =?", + new String[]{account.getUsername(), account.getHost()}); + } catch (Exception e) { + e.printStackTrace(); + return -1; + } + + } + + + /** + * Update an Account token in database + * + * @param token Token + * @return boolean + */ + public int updateAccountToken(Token token) { + ContentValues values = new ContentValues(); + if (token.getRefresh_token() != null) { + values.put(Sqlite.COL_REFRESH_TOKEN, token.getRefresh_token()); + } + if (token.getAccess_token() != null) + values.put(Sqlite.COL_OAUTHTOKEN, token.getAccess_token()); + SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null); + String instance = HelperInstance.getLiveInstance(context); + try { + return db.update(Sqlite.TABLE_USER_ACCOUNT, + values, Sqlite.COL_USER_ID + " = ? AND " + Sqlite.COL_INSTANCE + " =?", + new String[]{userId, instance}); + } catch (Exception e) { + e.printStackTrace(); + } + return -1; + } + + + /** + * Update an Account in database + * + * @param account Account + * @return boolean + */ + public int updateAccountCredential(Account account) { + ContentValues values = new ContentValues(); + if (account.getCreatedAt() == null) + account.setCreatedAt(new Date()); + if (account.getDescription() == null) + account.setDescription(""); + values.put(Sqlite.COL_USERNAME, account.getUsername()); + values.put(Sqlite.COL_ACCT, account.getUsername() + "@" + account.getHost()); + values.put(Sqlite.COL_DISPLAYED_NAME, account.getDisplayName()); + values.put(Sqlite.COL_FOLLOWERS_COUNT, account.getFollowersCount()); + values.put(Sqlite.COL_FOLLOWING_COUNT, account.getFollowingCount()); + values.put(Sqlite.COL_NOTE, account.getDescription()); + values.put(Sqlite.COL_URL, account.getUrl()); + values.put(Sqlite.COL_AVATAR, account.getAvatar()); + values.put(Sqlite.COL_CREATED_AT, Helper.dateToString(account.getCreatedAt())); + + if (account.getClient_id() != null && account.getClient_secret() != null) { + values.put(Sqlite.COL_CLIENT_ID, account.getClient_id()); + values.put(Sqlite.COL_CLIENT_SECRET, account.getClient_secret()); + } + if (account.getRefresh_token() != null) { + values.put(Sqlite.COL_REFRESH_TOKEN, account.getRefresh_token()); + } + if (account.getToken() != null) + values.put(Sqlite.COL_OAUTHTOKEN, account.getToken()); + return db.update(Sqlite.TABLE_USER_ACCOUNT, + values, Sqlite.COL_USER_ID + " = ? AND " + Sqlite.COL_INSTANCE + " =?", + new String[]{account.getId(), account.getHost()}); + } + + + public int removeUser(Account account) { + return db.delete(Sqlite.TABLE_USER_ACCOUNT, Sqlite.COL_USER_ID + " = '" + account.getId() + + "' AND " + Sqlite.COL_INSTANCE + " = '" + account.getHost() + "'", null); + } + + + /** + * Returns last used account + * + * @return Account + */ + public Account getLastUsedAccount() { + + try { + Cursor c = db.query(Sqlite.TABLE_USER_ACCOUNT, null, Sqlite.COL_OAUTHTOKEN + " != 'null'", null, null, null, Sqlite.COL_UPDATED_AT + " DESC", "1"); + return cursorToUser(c); + } catch (Exception e) { + return null; + } + } + + + /** + * Returns all Account in db + * + * @return Account List + */ + public List getAllAccount() { + + try { + Cursor c = db.query(Sqlite.TABLE_USER_ACCOUNT, null, null, null, null, null, Sqlite.COL_INSTANCE + " ASC", null); + return cursorToListUser(c); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + + /** + * Returns an Account by token + * + * @param token String + * @return Account + */ + public Account getAccountByToken(String token) { + + try { + Cursor c = db.query(Sqlite.TABLE_USER_ACCOUNT, null, Sqlite.COL_OAUTHTOKEN + " = \"" + token + "\"", null, null, null, null, "1"); + return cursorToUser(c); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + /** + * Returns an Account by id and instance + * + * @param id String + * @param instance String + * @return Account + */ + public Account getAccountByIdInstance(String id, String instance) { + + try { + Cursor c = db.query(Sqlite.TABLE_USER_ACCOUNT, null, Sqlite.COL_USER_ID + " = \"" + id + "\" AND " + Sqlite.COL_INSTANCE + " = \"" + instance + "\"", null, null, null, null, "1"); + return cursorToUser(c); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + /** + * Test if the current user is already stored in data base + * + * @param account Account + * @return boolean + */ + public boolean userExist(Account account) { + Cursor mCount = db.rawQuery("select count(*) from " + Sqlite.TABLE_USER_ACCOUNT + + " where " + Sqlite.COL_USERNAME + " = '" + account.getUsername() + "' AND " + Sqlite.COL_INSTANCE + " = '" + account.getHost() + "'", null); + mCount.moveToFirst(); + int count = mCount.getInt(0); + mCount.close(); + return (count > 0); + } + + + /*** + * Method to hydrate an Account from database + * @param c Cursor + * @return Account + */ + private Account cursorToUser(Cursor c) { + //No element found + + if (c.getCount() == 0) { + c.close(); + return null; + } + //Take the first element + c.moveToFirst(); + //New user + Account account = new Account(); + account.setId(c.getString(c.getColumnIndex(Sqlite.COL_USER_ID))); + account.setUsername(c.getString(c.getColumnIndex(Sqlite.COL_USERNAME))); + account.setDisplayName(c.getString(c.getColumnIndex(Sqlite.COL_DISPLAYED_NAME))); + account.setFollowersCount(c.getInt(c.getColumnIndex(Sqlite.COL_FOLLOWERS_COUNT))); + account.setFollowingCount(c.getInt(c.getColumnIndex(Sqlite.COL_FOLLOWING_COUNT))); + account.setDescription(c.getString(c.getColumnIndex(Sqlite.COL_NOTE))); + account.setUrl(c.getString(c.getColumnIndex(Sqlite.COL_URL))); + account.setAvatar(c.getString(c.getColumnIndex(Sqlite.COL_AVATAR))); + account.setUpdatedAt(Helper.stringToDate(context, c.getString(c.getColumnIndex(Sqlite.COL_UPDATED_AT)))); + account.setCreatedAt(Helper.stringToDate(context, c.getString(c.getColumnIndex(Sqlite.COL_CREATED_AT)))); + account.setHost(c.getString(c.getColumnIndex(Sqlite.COL_INSTANCE))); + account.setToken(c.getString(c.getColumnIndex(Sqlite.COL_OAUTHTOKEN))); + account.setSoftware(c.getString(c.getColumnIndex(Sqlite.COL_SOFTWARE))); + account.setClient_id(c.getString(c.getColumnIndex(Sqlite.COL_CLIENT_ID))); + account.setClient_secret(c.getString(c.getColumnIndex(Sqlite.COL_CLIENT_SECRET))); + account.setRefresh_token(c.getString(c.getColumnIndex(Sqlite.COL_REFRESH_TOKEN))); + //Close the cursor + c.close(); + + //User is returned + return account; + } + + + /*** + * Method to hydrate an Accounts from database + * @param c Cursor + * @return List + */ + private List cursorToListUser(Cursor c) { + //No element found + if (c.getCount() == 0) { + c.close(); + return null; + } + List accounts = new ArrayList<>(); + while (c.moveToNext()) { + //New user + Account account = new Account(); + account.setId(c.getString(c.getColumnIndex(Sqlite.COL_USER_ID))); + account.setUsername(c.getString(c.getColumnIndex(Sqlite.COL_USERNAME))); + account.setDisplayName(c.getString(c.getColumnIndex(Sqlite.COL_DISPLAYED_NAME))); + account.setFollowersCount(c.getInt(c.getColumnIndex(Sqlite.COL_FOLLOWERS_COUNT))); + account.setFollowingCount(c.getInt(c.getColumnIndex(Sqlite.COL_FOLLOWING_COUNT))); + account.setDescription(c.getString(c.getColumnIndex(Sqlite.COL_NOTE))); + account.setUrl(c.getString(c.getColumnIndex(Sqlite.COL_URL))); + account.setAvatar(c.getString(c.getColumnIndex(Sqlite.COL_AVATAR))); + account.setUpdatedAt(Helper.stringToDate(context, c.getString(c.getColumnIndex(Sqlite.COL_UPDATED_AT)))); + account.setCreatedAt(Helper.stringToDate(context, c.getString(c.getColumnIndex(Sqlite.COL_CREATED_AT)))); + account.setHost(c.getString(c.getColumnIndex(Sqlite.COL_INSTANCE))); + account.setToken(c.getString(c.getColumnIndex(Sqlite.COL_OAUTHTOKEN))); + account.setSoftware(c.getString(c.getColumnIndex(Sqlite.COL_SOFTWARE))); + account.setClient_id(c.getString(c.getColumnIndex(Sqlite.COL_CLIENT_ID))); + account.setClient_secret(c.getString(c.getColumnIndex(Sqlite.COL_CLIENT_SECRET))); + account.setRefresh_token(c.getString(c.getColumnIndex(Sqlite.COL_REFRESH_TOKEN))); + accounts.add(account); + } + //Close the cursor + c.close(); + //Users list is returned + return accounts; + } + + +} diff --git a/app/src/main/java/app/fedilab/fedilabtube/viewmodel/ChannelsVM.java b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/ChannelsVM.java index eccba46..405a295 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/viewmodel/ChannelsVM.java +++ b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/ChannelsVM.java @@ -63,7 +63,7 @@ public class ChannelsVM extends AndroidViewModel { if (type == RetrofitPeertubeAPI.DataType.MY_CHANNELS) { SharedPreferences sharedpreferences = _mContext.getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE); SQLiteDatabase db = Sqlite.getInstance(_mContext.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); - String token = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null); + String token = Helper.getToken(_mContext); AccountData.Account account = new AccountDAO(_mContext, db).getAccountByToken(token); finalElement = account.getUsername() + "@" + account.getHost(); } diff --git a/app/src/main/java/app/fedilab/fedilabtube/viewmodel/PlaylistsVM.java b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/PlaylistsVM.java index 9675907..3b60376 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/viewmodel/PlaylistsVM.java +++ b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/PlaylistsVM.java @@ -16,7 +16,6 @@ package app.fedilab.fedilabtube.viewmodel; import android.app.Application; import android.content.Context; -import android.content.SharedPreferences; import android.database.sqlite.SQLiteDatabase; import android.os.Handler; import android.os.Looper; @@ -97,8 +96,7 @@ public class PlaylistsVM extends AndroidViewModel { Context _mContext = getApplication().getApplicationContext(); new Thread(() -> { try { - SharedPreferences sharedpreferences = _mContext.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); - String token = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null); + String token = Helper.getToken(_mContext); SQLiteDatabase db = Sqlite.getInstance(_mContext.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); Account account = new AccountDAO(_mContext, db).getAccountByToken(token); int statusCode = -1;