Twidere-App-Android-Twitter.../twidere.component.common/src/main/java/org/mariotaku/twidere/model/ParcelableAccount.java

281 lines
11 KiB
Java
Raw Normal View History

2014-07-03 07:48:39 +02:00
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2014 Mariotaku Lee <mariotaku.lee@gmail.com>
*
* 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.twidere.model;
import android.content.Context;
import android.database.Cursor;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.NonNull;
2015-03-21 15:53:26 +01:00
import android.support.annotation.Nullable;
2014-07-03 07:48:39 +02:00
2015-04-23 17:09:13 +02:00
import com.bluelinelabs.logansquare.annotation.JsonField;
import com.bluelinelabs.logansquare.annotation.JsonObject;
import com.hannesdorfmann.parcelableplease.annotation.ParcelablePlease;
import com.hannesdorfmann.parcelableplease.annotation.ParcelableThisPlease;
2015-04-23 17:09:13 +02:00
2015-11-30 06:32:05 +01:00
import org.mariotaku.library.objectcursor.annotation.CursorField;
import org.mariotaku.library.objectcursor.annotation.CursorObject;
import org.mariotaku.sqliteqb.library.Columns.Column;
import org.mariotaku.sqliteqb.library.Expression;
import org.mariotaku.sqliteqb.library.RawItemArray;
import org.mariotaku.twidere.provider.TwidereDataStore.Accounts;
import org.mariotaku.twidere.util.TwitterContentUtils;
2014-07-03 07:48:39 +02:00
import org.mariotaku.twidere.util.content.ContentResolverUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
2015-12-13 13:17:26 +01:00
@CursorObject(valuesCreator = true)
@ParcelablePlease(allFields = false)
2015-04-23 17:09:13 +02:00
@JsonObject
public class ParcelableAccount implements Parcelable {
2014-07-03 07:48:39 +02:00
@ParcelableThisPlease
2015-04-23 17:09:13 +02:00
@JsonField(name = "screen_name")
2015-11-30 06:32:05 +01:00
@CursorField(Accounts.SCREEN_NAME)
2015-04-23 17:09:13 +02:00
public String screen_name;
@ParcelableThisPlease
2015-04-23 17:09:13 +02:00
@JsonField(name = "name")
2015-11-30 06:32:05 +01:00
@CursorField(Accounts.NAME)
2015-04-23 17:09:13 +02:00
public String name;
@ParcelableThisPlease
2015-04-23 17:09:13 +02:00
@JsonField(name = "profile_image_url")
2015-11-30 06:32:05 +01:00
@CursorField(Accounts.PROFILE_IMAGE_URL)
2015-04-23 17:09:13 +02:00
public String profile_image_url;
@ParcelableThisPlease
2015-04-23 17:09:13 +02:00
@JsonField(name = "profile_banner_url")
2015-11-30 06:32:05 +01:00
@CursorField(Accounts.PROFILE_BANNER_URL)
2015-04-23 17:09:13 +02:00
public String profile_banner_url;
@ParcelableThisPlease
2015-04-23 17:09:13 +02:00
@JsonField(name = "account_id")
2015-11-30 06:32:05 +01:00
@CursorField(Accounts.ACCOUNT_ID)
2015-04-23 17:09:13 +02:00
public long account_id;
@ParcelableThisPlease
2015-04-23 17:09:13 +02:00
@JsonField(name = "color")
2015-11-30 06:32:05 +01:00
@CursorField(Accounts.COLOR)
2015-04-23 17:09:13 +02:00
public int color;
@ParcelableThisPlease
2015-04-23 17:09:13 +02:00
@JsonField(name = "is_activated")
2015-11-30 06:32:05 +01:00
@CursorField(Accounts.IS_ACTIVATED)
2015-04-23 17:09:13 +02:00
public boolean is_activated;
public boolean is_dummy;
2015-11-30 06:32:05 +01:00
ParcelableAccount() {
}
2015-05-10 17:05:26 +02:00
public static ParcelableAccount dummyAccount() {
2015-11-30 06:32:05 +01:00
final ParcelableAccount account = new ParcelableAccount();
account.is_dummy = true;
return account;
}
2015-05-10 17:05:26 +02:00
public static ParcelableCredentials dummyCredentials() {
2015-11-30 06:32:05 +01:00
final ParcelableCredentials credentials = new ParcelableCredentials();
credentials.is_dummy = true;
return credentials;
2015-05-10 17:05:26 +02:00
}
2015-11-30 06:32:05 +01:00
public static ParcelableAccount getAccount(final Context context, final long accountId) {
if (context == null) return null;
final Cursor cur = ContentResolverUtils.query(context.getContentResolver(), Accounts.CONTENT_URI,
2015-11-30 06:32:05 +01:00
Accounts.COLUMNS, Expression.equals(Accounts.ACCOUNT_ID, accountId).getSQL(), null, null);
if (cur == null) return null;
try {
if (cur.moveToFirst()) {
return ParcelableAccountCursorIndices.fromCursor(cur);
}
2015-11-30 06:32:05 +01:00
} finally {
cur.close();
}
return null;
}
2015-05-04 08:23:47 +02:00
@NonNull
public static long[] getAccountIds(final ParcelableAccount[] accounts) {
final long[] ids = new long[accounts.length];
for (int i = 0, j = accounts.length; i < j; i++) {
ids[i] = accounts[i].account_id;
}
return ids;
}
2015-05-04 08:23:47 +02:00
@NonNull
public static ParcelableAccount[] getAccounts(final Context context, final boolean activatedOnly,
final boolean officialKeyOnly) {
final List<ParcelableAccount> list = getAccountsList(context, activatedOnly, officialKeyOnly);
return list.toArray(new ParcelableAccount[list.size()]);
}
2015-03-21 15:53:26 +01:00
@NonNull
public static ParcelableAccount[] getAccounts(@Nullable final Context context, @Nullable final long[] accountIds) {
if (context == null) return new ParcelableAccount[0];
final String where = accountIds != null ? Expression.in(new Column(Accounts.ACCOUNT_ID),
new RawItemArray(accountIds)).getSQL() : null;
final Cursor cur = ContentResolverUtils.query(context.getContentResolver(), Accounts.CONTENT_URI,
Accounts.COLUMNS_NO_CREDENTIALS, where, null, null);
if (cur == null) return new ParcelableAccount[0];
2015-11-30 06:32:05 +01:00
return getAccounts(cur, new ParcelableAccountCursorIndices(cur));
2015-03-21 15:53:26 +01:00
}
@NonNull
public static ParcelableAccount[] getAccounts(@Nullable final Cursor cursor) {
if (cursor == null) return new ParcelableAccount[0];
2015-11-30 06:32:05 +01:00
return getAccounts(cursor, new ParcelableAccountCursorIndices(cursor));
2015-03-21 15:53:26 +01:00
}
@NonNull
2015-11-30 06:32:05 +01:00
public static ParcelableAccount[] getAccounts(@Nullable final Cursor cursor, @Nullable final ParcelableAccountCursorIndices indices) {
2015-03-21 15:53:26 +01:00
if (cursor == null || indices == null) return new ParcelableAccount[0];
try {
2015-03-21 15:53:26 +01:00
cursor.moveToFirst();
final ParcelableAccount[] names = new ParcelableAccount[cursor.getCount()];
while (!cursor.isAfterLast()) {
2015-11-30 06:32:05 +01:00
names[cursor.getPosition()] = indices.newObject(cursor);
2015-03-21 15:53:26 +01:00
cursor.moveToNext();
}
return names;
} finally {
2015-03-21 15:53:26 +01:00
cursor.close();
}
}
public static List<ParcelableAccount> getAccountsList(final Context context, final boolean activatedOnly) {
return getAccountsList(context, activatedOnly, false);
}
public static List<ParcelableAccount> getAccountsList(final Context context, final boolean activatedOnly,
final boolean officialKeyOnly) {
if (context == null) return Collections.emptyList();
final ArrayList<ParcelableAccount> accounts = new ArrayList<>();
final Cursor cur = ContentResolverUtils.query(context.getContentResolver(),
Accounts.CONTENT_URI, Accounts.COLUMNS,
activatedOnly ? Accounts.IS_ACTIVATED + " = 1" : null, null, Accounts.SORT_POSITION);
if (cur == null) return accounts;
2015-11-30 06:32:05 +01:00
final ParcelableCredentialsCursorIndices indices = new ParcelableCredentialsCursorIndices(cur);
cur.moveToFirst();
while (!cur.isAfterLast()) {
if (!officialKeyOnly) {
2015-11-30 06:32:05 +01:00
accounts.add(indices.newObject(cur));
} else {
final String consumerKey = cur.getString(indices.consumer_key);
final String consumerSecret = cur.getString(indices.consumer_secret);
if (TwitterContentUtils.isOfficialKey(context, consumerKey, consumerSecret)) {
2015-11-30 06:32:05 +01:00
accounts.add(indices.newObject(cur));
}
}
cur.moveToNext();
}
cur.close();
return accounts;
}
2015-01-14 09:47:51 +01:00
public static ParcelableCredentials getCredentials(final Context context, final long accountId) {
if (context == null) return null;
Cursor cur = ContentResolverUtils.query(context.getContentResolver(), Accounts.CONTENT_URI,
Accounts.COLUMNS, Accounts.ACCOUNT_ID + " = " + accountId, null, null);
if (cur == null) return null;
try {
if (cur.moveToFirst()) {
2015-11-30 06:32:05 +01:00
return ParcelableCredentialsCursorIndices.fromCursor(cur);
}
} finally {
cur.close();
}
return null;
}
2015-01-14 09:47:51 +01:00
public static List<ParcelableCredentials> getCredentialsList(final Context context, final boolean activatedOnly) {
return getCredentialsList(context, activatedOnly, false);
}
2015-07-17 16:30:41 +02:00
public static ParcelableCredentials[] getCredentialsArray(final Context context, final boolean activatedOnly,
final boolean officialKeyOnly) {
final List<ParcelableCredentials> credentialsList = getCredentialsList(context, activatedOnly, officialKeyOnly);
return credentialsList.toArray(new ParcelableCredentials[credentialsList.size()]);
}
2015-01-14 09:47:51 +01:00
public static List<ParcelableCredentials> getCredentialsList(final Context context, final boolean activatedOnly,
final boolean officialKeyOnly) {
if (context == null) return Collections.emptyList();
final ArrayList<ParcelableCredentials> accounts = new ArrayList<>();
final Cursor cur = ContentResolverUtils.query(context.getContentResolver(),
Accounts.CONTENT_URI, Accounts.COLUMNS,
activatedOnly ? Accounts.IS_ACTIVATED + " = 1" : null, null, Accounts.SORT_POSITION);
if (cur == null) return accounts;
2015-11-30 06:32:05 +01:00
ParcelableCredentialsCursorIndices indices = new ParcelableCredentialsCursorIndices(cur);
2015-01-14 09:47:51 +01:00
cur.moveToFirst();
while (!cur.isAfterLast()) {
if (officialKeyOnly) {
final String consumerKey = cur.getString(indices.consumer_key);
final String consumerSecret = cur.getString(indices.consumer_secret);
if (TwitterContentUtils.isOfficialKey(context, consumerKey, consumerSecret)) {
2015-11-30 06:32:05 +01:00
accounts.add(indices.newObject(cur));
2015-01-14 09:47:51 +01:00
}
} else {
2015-11-30 06:32:05 +01:00
accounts.add(indices.newObject(cur));
2015-01-14 09:47:51 +01:00
}
cur.moveToNext();
}
cur.close();
return accounts;
}
@Override
public String toString() {
return "Account{screen_name=" + screen_name + ", name=" + name + ", profile_image_url=" + profile_image_url
+ ", profile_banner_url=" + profile_banner_url + ", account_id=" + account_id + ", color=" + color
+ ", is_activated=" + is_activated + ", is_dummy=" + is_dummy + "}";
}
2015-11-30 06:32:05 +01:00
@Override
public int describeContents() {
return 0;
}
2015-02-03 06:22:02 +01:00
2015-11-30 06:32:05 +01:00
@Override
public void writeToParcel(Parcel dest, int flags) {
ParcelableAccountParcelablePlease.writeToParcel(this, dest, flags);
}
public static final Creator<ParcelableAccount> CREATOR = new Creator<ParcelableAccount>() {
public ParcelableAccount createFromParcel(Parcel source) {
ParcelableAccount target = new ParcelableAccount();
ParcelableAccountParcelablePlease.readFromParcel(target, source);
return target;
}
public ParcelableAccount[] newArray(int size) {
return new ParcelableAccount[size];
}
};
2014-07-03 07:48:39 +02:00
}