inujini_.hatate.AccountListActivity.java Source code

Java tutorial

Introduction

Here is the source code for inujini_.hatate.AccountListActivity.java

Source

/**
 * 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;

import inujini_.hatate.adapter.AccountAdapter;
import inujini_.hatate.data.TwitterAccount;
import inujini_.hatate.function.Function.Action;
import inujini_.hatate.function.Function.Action1;
import inujini_.hatate.function.Function.Func1;
import inujini_.hatate.reactive.ReactiveAsyncTask;
import inujini_.hatate.service.CallbackBroadcastReceiver;
import inujini_.hatate.service.OauthService;
import inujini_.hatate.sqlite.dao.AccountDao;

import java.util.List;

import lombok.val;
import twitter4j.auth.AccessToken;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.Toast;

/**
 * Twitter??????.
 */
public class AccountListActivity extends ListActivity implements OnItemLongClickListener {

    /**
     * <p>Oauth??.<p>
     * <p>??????null??</p>
     * <p>???null?????????
     * {@link Context#unregisterReceiver(android.content.BroadcastReceiver)}???</p>
     */
    private CallbackBroadcastReceiver _receiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);

        val prog = new ProgressDialog(this);
        prog.setMessage("??????...");
        prog.setCancelable(false);
        prog.setProgressStyle(ProgressDialog.STYLE_SPINNER);

        new ReactiveAsyncTask<Context, Void, List<TwitterAccount>>(new Func1<Context, List<TwitterAccount>>() {
            @Override
            public List<TwitterAccount> call(Context context) {
                return AccountDao.getAllAccount(context);
            }
        }).setOnPreExecute(new Action() {
            @Override
            public void call() {
                prog.show();
            }
        }).setOnPostExecute(new Action1<List<TwitterAccount>>() {
            @Override
            public void call(List<TwitterAccount> x) {
                if (prog != null && prog.isShowing())
                    prog.dismiss();

                val lv = getListView();
                lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
                lv.setOnItemLongClickListener(AccountListActivity.this);

                val adapter = new AccountAdapter(getApplicationContext(), x);
                setListAdapter(adapter);

                // set default checkbox's value
                for (int i = 0, count = adapter.getCount(); i < count; i++) {
                    lv.setItemChecked(i, adapter.getItem(i).isUse());
                }

            }
        }).setOnError(new Action1<Exception>() {
            @Override
            public void call(Exception e) {
                e.printStackTrace();
                if (prog != null && prog.isShowing())
                    prog.dismiss();

                Toast.makeText(getApplicationContext(), "??????", Toast.LENGTH_SHORT)
                        .show();
            }
        }).execute(getApplicationContext());
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        val adapter = (AccountAdapter) getListAdapter();
        val item = adapter.getItem(position);
        item.setUse(!item.isUse());
        AccountDao.setUseFlagAsync(item, getApplicationContext());
        adapter.notifyDataSetChanged();
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        val adapter = (AccountAdapter) getListAdapter();
        val item = adapter.getItem(position);

        new AlertDialog.Builder(this).setTitle("?")
                .setMessage(String.format("%s?????????", item.getScreenName()))
                .setPositiveButton("OK", new OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        AccountDao.deleteAsync(item, getApplicationContext());
                        adapter.remove(item);

                        // ???Twitter??
                        if (adapter.getCount() == 0) {
                            PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit()
                                    .putBoolean("isTweet", false).commit();
                        }
                    }
                }).setNegativeButton("", new OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).create().show();
        return true;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.menu_account_list, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        // add twitter account
        case R.id.menu_item_add:
            if (_receiver != null) {
                unregisterReceiver(_receiver);
                _receiver = null;
            }

            val prog = new ProgressDialog(this);
            prog.setMessage("OAuth??????????????????");
            prog.setProgressStyle(ProgressDialog.STYLE_SPINNER);

            _receiver = new CallbackBroadcastReceiver() {
                @Override
                public void onSuccess(AccessToken token) {
                    _receiver = null;
                    if (prog != null && prog.isShowing())
                        prog.dismiss();

                    if (AccountDao.isExist(token.getUserId(), getApplicationContext())) {
                        Toast.makeText(getApplicationContext(),
                                "??????????",
                                Toast.LENGTH_SHORT).show();
                        return;
                    }

                    Toast.makeText(getApplicationContext(),
                            "OAuth??????\n??????????????",
                            Toast.LENGTH_SHORT).show();
                    val accountData = new TwitterAccount();
                    accountData.setScreenName(token.getScreenName());
                    accountData.setAccessToken(token.getToken());
                    accountData.setAccessSecret(token.getTokenSecret());
                    accountData.setUse(true);
                    accountData.setUserId(token.getUserId());

                    AccountDao.insertAsync(accountData, getApplicationContext());
                    ((AccountAdapter) getListAdapter()).add(accountData);
                }

                @Override
                public void onError(Exception exception) {
                    _receiver = null;
                    if (prog != null && prog.isShowing())
                        prog.dismiss();

                    Toast.makeText(getApplicationContext(), "Oauth??????", Toast.LENGTH_LONG)
                            .show();
                }
            };

            registerReceiver(_receiver, CallbackBroadcastReceiver.createIntentFilter());

            // Oauth?
            val res = getApplicationContext().getResources();
            startService(OauthService.createIntent(res.getString(R.string.consumer_key),
                    res.getString(R.string.consumer_secret), getApplicationContext()));
            prog.show();

            return true;
        default:
            return false;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // Note: ?????????_receiver?????
        if (keyCode == KeyEvent.KEYCODE_BACK && _receiver != null) {
            unregisterReceiver(_receiver);
            _receiver = null;
        }
        return super.onKeyDown(keyCode, event);
    }

}