Java tutorial
/** * Copyright 2012 C. A. Fitzgerald * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.github.riotopsys.shoppinglist.activity; import java.util.UUID; import roboguice.activity.RoboFragmentActivity; import roboguice.inject.InjectView; import android.accounts.AccountManager; import android.accounts.AccountManagerCallback; import android.accounts.AccountManagerFuture; import android.app.AlertDialog; import android.content.BroadcastReceiver; import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.content.Intent; import android.content.IntentFilter; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.view.ViewPager; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import com.github.riotopsys.shoppinglist.AppKeys; import com.github.riotopsys.shoppinglist.Configurations; import com.github.riotopsys.shoppinglist.IConfigurations; import com.github.riotopsys.shoppinglist.R; import com.github.riotopsys.shoppinglist.adapter.ShoppingListCollectionAdapter; import com.github.riotopsys.shoppinglist.model.ShoppingList; import com.github.riotopsys.shoppinglist.model.ShoppingListCollection; import com.github.riotopsys.shoppinglist.server.ServerInterfaceService; import com.github.riotopsys.shoppinglist.server.ServerTask; import com.github.riotopsys.shoppinglist.utils.AccountUtils; import com.google.android.gcm.GCMRegistrar; import com.google.inject.Inject; public class ShoppingListPreview extends RoboFragmentActivity { private static final String TAG = ShoppingListPreview.class.getSimpleName(); ShoppingListCollection mShoppingListCollection; ShoppingListCollectionAdapter mShoppingListCollectionAdapter; @InjectView(R.id.pager) ViewPager mViewPager; private BroadcastReceiver mListUpdateReciever = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { mShoppingListCollectionAdapter.notifyDataSetChanged(context); } }; private IntentFilter mFilter = new IntentFilter(AppKeys.LIST_UPDATE_ACTION); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_shopping_list_preview); AccountUtils accountUtils = new AccountUtils(); String account = accountUtils.getAccountName(this); if (account == null) { AccountManager am = AccountManager.get(this); am.getAuthTokenByFeatures(AppKeys.ACCOUNT_TYPE, AppKeys.OAUTH2_SCOPE, null, this, null, null, new AccountManagerCallback<Bundle>() { @Override public void run(AccountManagerFuture<Bundle> future) { try { Bundle bundle = future.getResult(); Log.i(TAG, "Got Bundle:\n" + " act name: " + bundle.getString(AccountManager.KEY_ACCOUNT_NAME) + "\n act type: " + bundle.getString(AccountManager.KEY_ACCOUNT_TYPE) + "\n auth token: " + bundle.getString(AccountManager.KEY_AUTHTOKEN)); AccountUtils accountUtils = new AccountUtils(); accountUtils.setAccountName(getBaseContext(), bundle.getString(AccountManager.KEY_ACCOUNT_NAME)); accountUtils.setToken(getBaseContext(), bundle.getString(AccountManager.KEY_AUTHTOKEN)); } catch (Exception e) { Log.i(TAG, "getAuthTokenByFeatures() cancelled or failed:", e); Toast.makeText(getBaseContext(), R.string.no_account, Toast.LENGTH_LONG).show(); finish(); } } }, null); } mShoppingListCollection = new ShoppingListCollection(); mShoppingListCollectionAdapter = new ShoppingListCollectionAdapter(this, getSupportFragmentManager(), mShoppingListCollection); // mViewPager = (ViewPager) findViewById(R.id.pager); mViewPager.setAdapter(mShoppingListCollectionAdapter); Intent startingIntent = getIntent(); if (startingIntent != null) { Uri data = startingIntent.getData(); if (data != null) { UUID guid = UUID.fromString(data.getLastPathSegment()); Intent i = new Intent(this, ServerInterfaceService.class); i.putExtra(AppKeys.SERVER_TASK_KEY, ServerTask.SUBSCRIBE); i.putExtra(AppKeys.GUID_KEY, guid); startService(i); } } IConfigurations config = new Configurations(); GCMRegistrar.checkDevice(this); GCMRegistrar.checkManifest(this); final String regId = GCMRegistrar.getRegistrationId(this); if (regId.equals("")) { GCMRegistrar.register(this, config.getGCMSenderID()); } else { Log.v(TAG, "Already registered"); } } @Override protected void onResume() { registerReceiver(mListUpdateReciever, mFilter); super.onResume(); } @Override protected void onPause() { unregisterReceiver(mListUpdateReciever); super.onPause(); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_shopping_list_preview, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_remove_list: removeList(); break; case R.id.menu_new_list: addNewList(); break; case R.id.menu_rename_list: renameList(); break; case R.id.menu_refresh: refreshData(); break; case R.id.menu_add_item: break; default: Log.e(TAG, "unsupported menu item"); break; } return super.onOptionsItemSelected(item); } private void refreshData() { Intent i = new Intent(this, ServerInterfaceService.class); i.putExtra(AppKeys.SERVER_TASK_KEY, ServerTask.SYNC); startService(i); } private void removeList() { ShoppingList list; if (mShoppingListCollectionAdapter.getCount() == 0) { return; } list = mShoppingListCollectionAdapter.getList(mViewPager.getCurrentItem()); mShoppingListCollection.removeItem(this, list); mShoppingListCollectionAdapter.notifyDataSetChanged(this); } private void addNewList() { LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.add_list_dialog, null); new AlertDialog.Builder(this).setTitle(R.string.add_list_dialog_title).setView(view) .setPositiveButton(R.string.done, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { EditText editText = (EditText) ((AlertDialog) dialog).findViewById(R.id.list_name); String text = editText.getText().toString(); if (!text.equals("")) { ShoppingList shoppingList = mShoppingListCollection .createItem(ShoppingListPreview.this); shoppingList.setName(ShoppingListPreview.this, text); mShoppingListCollectionAdapter.notifyDataSetChanged(editText.getContext()); mViewPager.setCurrentItem(mShoppingListCollectionAdapter.getPositionOf(shoppingList), true); } else { Toast.makeText(getBaseContext(), R.string.name_reqd, Toast.LENGTH_LONG).show(); } } }).setNegativeButton(R.string.cancel, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }).create().show(); } private void renameList() { ShoppingList list; if (mShoppingListCollectionAdapter.getCount() == 0) { return; } list = mShoppingListCollectionAdapter.getList(mViewPager.getCurrentItem()); LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.add_list_dialog, null); ((TextView) view.findViewById(R.id.list_name)).setText(list.getName()); new AlertDialog.Builder(this).setTitle(R.string.add_list_dialog_title).setView(view) .setPositiveButton(R.string.done, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { EditText editText = (EditText) ((AlertDialog) dialog).findViewById(R.id.list_name); String text = editText.getText().toString(); if (!text.equals("")) { ShoppingList shoppingList = mShoppingListCollectionAdapter .getList(mViewPager.getCurrentItem()); shoppingList.setName(ShoppingListPreview.this, text); mShoppingListCollectionAdapter.notifyDataSetChanged(editText.getContext()); } else { Toast.makeText(getBaseContext(), R.string.name_reqd, Toast.LENGTH_LONG).show(); } } }).setNegativeButton(R.string.cancel, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }).create().show(); } }