diff options
Diffstat (limited to 'app/src/main/java/net/lacolaco/smileessence/activity/ManageAccountsActivity.kt')
-rw-r--r-- | app/src/main/java/net/lacolaco/smileessence/activity/ManageAccountsActivity.kt | 241 |
1 files changed, 241 insertions, 0 deletions
diff --git a/app/src/main/java/net/lacolaco/smileessence/activity/ManageAccountsActivity.kt b/app/src/main/java/net/lacolaco/smileessence/activity/ManageAccountsActivity.kt new file mode 100644 index 00000000..2ef184fc --- /dev/null +++ b/app/src/main/java/net/lacolaco/smileessence/activity/ManageAccountsActivity.kt @@ -0,0 +1,241 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2012-2014 lacolaco.net + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.lacolaco.smileessence.activity + +import android.app.Activity +import android.content.Intent +import android.content.pm.PackageManager +import android.net.Uri +import android.os.Bundle +import android.support.v4.app.ActivityCompat +import android.support.v4.content.ContextCompat +import android.view.Menu +import android.view.MenuItem +import android.view.View +import android.view.ViewGroup +import android.widget.* +import com.android.volley.toolbox.NetworkImageView +import net.lacolaco.smileessence.Application +import net.lacolaco.smileessence.R +import net.lacolaco.smileessence.World +import net.lacolaco.smileessence.data.Account +import net.lacolaco.smileessence.data.ImageCache +import net.lacolaco.smileessence.logging.Logger +import net.lacolaco.smileessence.twitter.OAuthSession +import net.lacolaco.smileessence.view.dialog.ConfirmDialogFragment + +import java.util.ArrayList + +class ManageAccountsActivity : Activity(), AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener { + private var adapter: EditAccountsAdapter? = null + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + + // Check if it is initiated from launcher + if (!intent.getBooleanExtra(INTENT_KEY_NOINIT, false)) { + val wextPermission = ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) + if (wextPermission != PackageManager.PERMISSION_GRANTED) { + ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.WRITE_EXTERNAL_STORAGE), REQUEST_WRITE_EXTERNAL_STORAGE_PERMISSION) + } + + // Skip this activity if app is already started + val currentWorld = Application.currentWorld + if (currentWorld != null) { + goToWorld(currentWorld) + return + } + } + + setContentView(R.layout.layout_edit_list) + adapter = EditAccountsAdapter() + val listView = findViewById(R.id.listview_edit_list) as ListView + listView.adapter = adapter + listView.onItemClickListener = this + listView.onItemLongClickListener = this + } + + override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) { + when (requestCode) { + REQUEST_WRITE_EXTERNAL_STORAGE_PERMISSION -> { + if (grantResults.size > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { + // OK + val currentWorld = Application.currentWorld + if (currentWorld != null) { + goToWorld(currentWorld) + } + } else { + Application.toast(R.string.notice_error_storage_permission) + // TODO: Kill Process? + finish() + } + } + } + } + + private fun goToWorld(world: World) { + // Continue the existing MainActivity + val intent = Intent(this, MainActivity::class.java) + intent.data = Uri.parse("smileessence://mainactivity/?user_id=" + world.account.userId) + finish() + startActivity(intent) + } + + override fun onCreateOptionsMenu(menu: Menu): Boolean { + val add = menu.add(Menu.NONE, R.id.menu_edit_list_add, Menu.NONE, "") + add.setIcon(android.R.drawable.ic_menu_add) + add.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM) + return true + } + + override fun onItemClick(adapterView: AdapterView<*>, view: View, i: Int, l: Long) { + val account = adapter!!.getItem(i) + goToWorld(Application.getWorld(account.userId)) + } + + override fun onItemLongClick(adapterView: AdapterView<*>, view: View, i: Int, l: Long): Boolean { + if (adapter!!.count > 1) { + // remove account from application + val account = adapter!!.getItem(i) + ConfirmDialogFragment.show(this, getString(R.string.dialog_confirm_clear_account, account.user.screenName), { + adapter!!.removeAt(i) + Account.unregister(account.userId) + }, false) + return true + } else { + Application.toast(R.string.notice_cant_remove_last_account) + return false + } + } + + override fun onOptionsItemSelected(item: MenuItem): Boolean { + when (item.itemId) { + R.id.menu_edit_list_add -> { + startActivityForResult(Intent(this, OAuthActivity::class.java), REQUEST_OAUTH) + } + android.R.id.home -> { + safeFinish() + } + } + return true + } + + override fun onBackPressed() { + safeFinish() + } + + private fun safeFinish() { + val currentWorld = Application.currentWorld + + if (currentWorld != null) { + goToWorld(currentWorld) + } else { + setResult(Activity.RESULT_CANCELED) + finish() + } + } + + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) { + when (requestCode) { + REQUEST_OAUTH -> { + receiveOAuth(requestCode, resultCode, data) + } + else -> { + Logger.error("[BUG] unexpected activity result: reqCode=$requestCode, resCode=$resultCode") + } + } + } + + private fun receiveOAuth(requestCode: Int, resultCode: Int, data: Intent) { + if (resultCode == Activity.RESULT_OK) { + val account = Account.register(data.getStringExtra(OAuthSession.KEY_TOKEN), + data.getStringExtra(OAuthSession.KEY_TOKEN_SECRET), + data.getLongExtra(OAuthSession.KEY_USER_ID, -1L), + data.getStringExtra(OAuthSession.KEY_SCREEN_NAME)) + adapter!!.add(account) + } else { + Logger.error(requestCode) + Application.toast(R.string.notice_error_authenticate) + } + } + + private inner class EditAccountsAdapter : BaseAdapter() { + private val accounts: MutableList<Account> + + init { + accounts = ArrayList(Account.all()) + } + + override fun getCount(): Int { + return accounts.size + } + + override fun getItem(position: Int): Account { + return accounts[position] + } + + override fun getItemId(position: Int): Long { + return accounts[position].userId + } + + override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { + var convertView = convertView + if (convertView == null) { + convertView = layoutInflater.inflate(R.layout.list_item_account, null) + } + val account = getItem(position) + val iconView = convertView!!.findViewById(R.id.account_icon) as NetworkImageView + iconView.setImageUrl(account.user.profileImageUrl, ImageCache.getImageLoader()) + + val textView = convertView.findViewById(R.id.account_text_view) as TextView + val text = "@" + account.user.screenName + textView.text = text + + return convertView + } + + fun add(account: Account): Int { + if (!accounts.contains(account)) { + accounts.add(account) + notifyDataSetChanged() + return accounts.size - 1 + } else { + return accounts.indexOf(account) + } + } + + fun removeAt(position: Int): Account? { + val account = accounts.removeAt(position) + notifyDataSetChanged() + return account + } + } + + companion object { + val INTENT_KEY_NOINIT = "noInit" + private val REQUEST_OAUTH = 10 + private val REQUEST_WRITE_EXTERNAL_STORAGE_PERMISSION = 11 + } +} |