Java tutorial
/** * HatateHoutyouAlarm * * Copyright (c) 2014 @inujini_ (https://twitter.com/inujini_) * * This software is released under the MIT License. * http://opensource.org/licenses/mit-license.php */ package inujini_.hatate.sqlite.dao; import inujini_.hatate.R; import inujini_.hatate.data.TwitterAccount; import inujini_.hatate.data.meta.MetaAccount; import inujini_.hatate.function.Function.Func1; import inujini_.hatate.reactive.ReactiveAsyncTask; import inujini_.hatate.sqlite.DatabaseHelper; import inujini_.hatate.sqlite.helper.ColumnValuePair; import inujini_.hatate.sqlite.helper.CursorExtensions; import inujini_.hatate.sqlite.helper.QueryBuilder; import inujini_.hatate.sqlite.helper.SqliteUtil; import java.util.List; import lombok.val; import lombok.experimental.ExtensionMethod; import twitter4j.Twitter; import twitter4j.TwitterFactory; import twitter4j.conf.ConfigurationBuilder; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; /** * {@link TwitterAccount}?DAO */ @ExtensionMethod({ SqliteUtil.class, CursorExtensions.class }) public class AccountDao { /** * ?Twitter?. * @param context * @return ?????????{@link Twitter} */ public static List<Twitter> getTwitter(Context context) { val q = new QueryBuilder().selectAll().from(MetaAccount.TBL_NAME).where().equal(MetaAccount.UseFlag, true) .toString(); val res = context.getResources(); val consumerKey = res.getString(R.string.consumer_key); val consumerSecret = res.getString(R.string.consumer_secret); return new DatabaseHelper(context).getList(q, context, new Func1<Cursor, Twitter>() { @Override public Twitter call(Cursor c) { return new TwitterFactory(new ConfigurationBuilder().setOAuthConsumerKey(consumerKey) .setOAuthConsumerSecret(consumerSecret) .setOAuthAccessToken(c.getStringByMeta(MetaAccount.AccessToken)) .setOAuthAccessTokenSecret(c.getStringByMeta(MetaAccount.AccessSecret)) .setHttpConnectionTimeout(15000).setHttpReadTimeout(30000).build()).getInstance(); } }); } /** * ?? * @param context * @return ?? */ public static int getCount(Context context) { val q = new QueryBuilder().select(MetaAccount.Id).from(MetaAccount.TBL_NAME).toString(); return new DatabaseHelper(context).getList(q, context, new Func1<Cursor, Integer>() { @Override public Integer call(Cursor arg0) { return 0; } }).size(); } /** * ?. * @param context * @return {@link TwitterAccount}? */ public static List<TwitterAccount> getAllAccount(Context context) { return new DatabaseHelper(context).selectAll(context, MetaAccount.TBL_NAME, new Func1<Cursor, TwitterAccount>() { @Override public TwitterAccount call(Cursor c) { val a = new TwitterAccount(); a.setScreenName(c.getStringByMeta(MetaAccount.ScreenName)); a.setUserId(Long.parseLong(c.getStringByMeta(MetaAccount.UserId))); a.setUse(c.getBooleanByMeta(MetaAccount.UseFlag)); return a; } }); } /** * . * @param account ? * @param context */ public static void insert(TwitterAccount account, Context context) { val values = new ContentValues(); values.put(MetaAccount.ScreenName.getColumnName(), account.getScreenName()); values.put(MetaAccount.UserId.getColumnName(), account.getUserId()); values.put(MetaAccount.AccessToken.getColumnName(), account.getAccessToken()); values.put(MetaAccount.AccessSecret.getColumnName(), account.getAccessSecret()); new DatabaseHelper(context).insert(context, MetaAccount.TBL_NAME, values); } /** * (??). * @param account ? * @param context */ public static void insertAsync(final TwitterAccount account, Context context) { new ReactiveAsyncTask<Context, Void, Void>(new Func1<Context, Void>() { @Override public Void call(Context x) { AccountDao.insert(account, x); return null; } }).execute(context); } /** * . * @param userId Twitter?userId * @param isUse * @param context */ public static void setUseFlag(long userId, boolean isUse, Context context) { val cv = new ContentValues(); cv.put(MetaAccount.UseFlag.getColumnName(), (isUse ? 1 : 0)); new DatabaseHelper(context).update(context, MetaAccount.TBL_NAME, cv, new ColumnValuePair(MetaAccount.UserId, userId)); } /** * . * @param account * @param context */ public static void setUseFlag(TwitterAccount account, Context context) { setUseFlag(account.getUserId(), account.isUse(), context); } /** * (??). * @param account * @param context */ public static void setUseFlagAsync(final TwitterAccount account, Context context) { new ReactiveAsyncTask<Context, Void, Void>(new Func1<Context, Void>() { @Override public Void call(Context x) { AccountDao.setUseFlag(account, x); return null; } }).execute(context); } /** * . * @param account * @param context */ public static void delete(TwitterAccount account, Context context) { new DatabaseHelper(context).delete(context, MetaAccount.TBL_NAME, new ColumnValuePair(MetaAccount.UserId, account.getUserId())); } /** * (??). * @param account * @param context */ public static void deleteAsync(final TwitterAccount account, Context context) { new ReactiveAsyncTask<Context, Void, Void>(new Func1<Context, Void>() { @Override public Void call(Context x) { AccountDao.delete(account, x); return null; } }).execute(context); } /** * ???. * @param context * @return ?1?????true */ public static boolean isAuthorized(Context context) { return getCount(context) != 0; } /** * ??. * @param userId ?????user_id * @param context * @return userId??????true */ public static boolean isExist(long userId, Context context) { val q = new QueryBuilder().select(MetaAccount.UserId).from(MetaAccount.TBL_NAME).where() .equal(MetaAccount.UserId, userId).toString(); return new DatabaseHelper(context).get(q, context, new Func1<Cursor, Long>() { @Override public Long call(Cursor c) { return Long.parseLong(c.getStringByMeta(MetaAccount.UserId)); } }) != null; } }