Java tutorial
/* * Copyright 2012 Mike Niyonkuru * * 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 net.niyonkuru.koodroid.ui; import android.app.Activity; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.LoaderManager; import android.support.v4.content.CursorLoader; import android.support.v4.content.Loader; import android.text.format.DateUtils; import android.view.LayoutInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.sql.Date; import java.sql.Timestamp; import net.niyonkuru.koodroid.R; import net.niyonkuru.koodroid.provider.AccountContract.Bills; import net.niyonkuru.koodroid.provider.AccountContract.Subscribers; import net.niyonkuru.koodroid.provider.AccountContract.Tabs; import net.niyonkuru.koodroid.provider.SettingsContract.Settings; import net.niyonkuru.koodroid.service.SyncService; import net.niyonkuru.koodroid.util.AnimUtils; import net.niyonkuru.koodroid.util.RelativeTime; /** * Displays a {@link Subscribers} name, {@link Bills} and {@link Tabs} information. */ public class OverviewFragment extends PageFragment { private static final int SUBSCRIBER_TOKEN = 0x1; private static final int BILL_TOKEN = 0x2; private static final int TAB_TOKEN = 0x3; private CallBack mCallBack; private TextView mSubscriberName; private TextView mBillTotal; private TextView mBillPastDueBalance; private TextView mBillCurrentBalance; private TextView mBillDueDate; private TextView mBillUpdateTime; private TextView mTabBalance; private TextView mTabUpdateTime; public interface CallBack extends BaseCallBack { public void onShowTabTransactions(String subscriber); } @Override public void onAttach(Activity activity) { super.onAttach(activity); try { mCallBack = (CallBack) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement CallBack type"); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View root = inflater.inflate(R.layout.overview_fragment, container, false); root.findViewById(R.id.bill_title).setOnClickListener(this); root.findViewById(R.id.bill_options).setOnClickListener(this); if (isBillEnabled()) { root.findViewById(R.id.bill_content).setVisibility(View.VISIBLE); } root.findViewById(R.id.tab_title).setOnClickListener(this); root.findViewById(R.id.tab_options).setOnClickListener(this); if (isTabEnabled()) { root.findViewById(R.id.tab_content).setVisibility(View.VISIBLE); } mSubscriberName = (TextView) root.findViewById(R.id.subscriber_name); mBillTotal = (TextView) root.findViewById(R.id.bill_total); mBillPastDueBalance = (TextView) root.findViewById(R.id.bill_past_due_balance); mBillCurrentBalance = (TextView) root.findViewById(R.id.bill_current_balance); mBillDueDate = (TextView) root.findViewById(R.id.bill_due_date); mBillUpdateTime = (TextView) root.findViewById(R.id.bill_update_time); mTabBalance = (TextView) root.findViewById(R.id.tab_total); mTabUpdateTime = (TextView) root.findViewById(R.id.tab_update_time); return root; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); LoaderManager loaderManager = getLoaderManager(); loaderManager.initLoader(SUBSCRIBER_TOKEN, null, this); if (isBillEnabled()) { loaderManager.initLoader(BILL_TOKEN, null, this); } if (isTabEnabled()) { loaderManager.initLoader(TAB_TOKEN, null, this); } } @Override public void onDestroyView() { super.onDestroyView(); LoaderManager loaderManager = getLoaderManager(); loaderManager.destroyLoader(SUBSCRIBER_TOKEN); if (isBillEnabled()) { loaderManager.destroyLoader(BILL_TOKEN); } if (isTabEnabled()) { loaderManager.destroyLoader(TAB_TOKEN); } } @Override public void onClick(View v) { int id = v.getId(); switch (id) { case R.id.bill_options: { PopupMenuBuilder builder = new PopupMenuBuilder(mContext, v); if (isBillEnabled()) { builder.addMenuResource(R.menu.bill_overflow_menu); builder.addMenuItemResource(R.id.disable_bill, R.string.disable); if (isBillDateSet()) { builder.addMenuItemResource(R.id.download_bill, R.string.download); } } else { builder.addMenuItemResource(R.id.enable_bill, R.string.enable); } builder.show(this); break; } case R.id.tab_options: PopupMenuBuilder builder = new PopupMenuBuilder(mContext, v); if (isTabEnabled()) { builder.addMenuResource(R.menu.tab_overflow_menu); builder.addMenuItemResource(R.id.disable_tab, R.string.disable); } else { builder.addMenuItemResource(R.id.enable_tab, R.string.enable); } builder.show(this); break; default: super.onClick(v); } } @Override public boolean onMenuItemClick(MenuItem item) { int id = item.getItemId(); switch (id) { case R.id.enable_bill: getLoaderManager().restartLoader(BILL_TOKEN, null, this); case R.id.disable_bill: View billContent = getView().findViewById(R.id.bill_content); AnimUtils.show(billContent); toggleService(Settings.BILL_SERVICE, id == R.id.enable_bill); return true; case R.id.refresh_bill: syncBill(); return true; case R.id.download_bill: sync(SyncService.DOWNLOAD_EBILL); return true; case R.id.enable_tab: getLoaderManager().restartLoader(TAB_TOKEN, null, this); case R.id.disable_tab: View tabContent = getView().findViewById(R.id.tab_content); AnimUtils.show(tabContent); toggleService(Settings.TAB_SERVICE, id == R.id.enable_tab); return true; case R.id.refresh_tab: sync(SyncService.TAB); return true; case R.id.detail_tab: mCallBack.onShowTabTransactions(getSubscriber()); return true; } return false; } @Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { switch (id) { case SUBSCRIBER_TOKEN: { final Uri uri = Subscribers.buildSubscriberUri(getSubscriber()); return new CursorLoader(mContext, uri, SubscribersQuery.PROJECTION, null, null, null); } case BILL_TOKEN: { final Uri uri = Bills.buildBillUri(getEmail()); return new CursorLoader(mContext, uri, BillsQuery.PROJECTION, null, null, null); } case TAB_TOKEN: { final Uri uri = Tabs.buildTabUri(getSubscriber()); CursorLoader loader = new CursorLoader(mContext, uri, TabsQuery.PROJECTION, null, null, null); loader.setUpdateThrottle(DateUtils.SECOND_IN_MILLIS); return loader; } } return null; } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor data) { if (!data.moveToFirst()) return; int id = loader.getId(); switch (id) { case SUBSCRIBER_TOKEN: { mSubscriberName.setText(data.getString(SubscribersQuery.FULL_NAME)); break; } case BILL_TOKEN: { mBillTotal.setText(formatMoney(data.getString(BillsQuery.TOTAL))); mBillPastDueBalance.setText(formatMoney(data.getString(BillsQuery.PAST_DUE_BALANCE))); mBillCurrentBalance.setText(formatMoney(data.getString(BillsQuery.CURRENT_CHARGES))); if (!data.isNull(BillsQuery.DUE_DATE)) { mBillDueDate.setTag(Date.valueOf(data.getString(BillsQuery.DUE_DATE))); } mBillUpdateTime.setTag(Timestamp.valueOf(data.getString(BillsQuery.UPDATED))); break; } case TAB_TOKEN: { mTabBalance.setText(formatMoney(data.getString(TabsQuery.BALANCE))); mTabUpdateTime.setTag(Timestamp.valueOf(data.getString(TabsQuery.UPDATED))); break; } } updateTimestamps(); } @Override protected void updateTimestamps() { RelativeTime t = new RelativeTime(mContext.getResources()); t.format(mBillDueDate); t.format(mBillUpdateTime); t.format(mTabUpdateTime); } @Override public void onLoaderReset(Loader<Cursor> loader) { int id = loader.getId(); switch (id) { case BILL_TOKEN: { mBillDueDate.setText(null); mBillUpdateTime.setText(null); } case TAB_TOKEN: { mTabUpdateTime.setText(null); } } } @Override protected void sync() { if (isBillEnabled()) { syncBill(); } if (isTabEnabled()) { sync(SyncService.TAB); } } private void syncBill() { if (!isBillDateSet()) { sync(SyncService.EBILL); } sync(SyncService.BILLING); } private boolean isBillEnabled() { return mSettings.serviceAllowed(getSubscriber(), Settings.BILL_SERVICE); } private boolean isBillDateSet() { return mSettings.eBillDay() != 0; } private boolean isTabEnabled() { return mSettings.serviceAllowed(getSubscriber(), Settings.TAB_SERVICE); } /** * {@link Subscribers} query parameters */ private interface SubscribersQuery { String[] PROJECTION = { Subscribers.SUBSCRIBER_FULL_NAME, }; int FULL_NAME = 0; } /** * {@link Bills} query parameters */ private interface BillsQuery { String[] PROJECTION = { Bills.UPDATED_LOCAL, Bills.BILL_TOTAL, Bills.BILL_PAST_DUE_BALANCE, Bills.BILL_CURRENT_CHARGES, Bills.BILL_DUE_DATE }; int UPDATED = 0; int TOTAL = 1; int PAST_DUE_BALANCE = 2; int CURRENT_CHARGES = 3; int DUE_DATE = 4; } /** * {@link Tabs} query parameters */ private interface TabsQuery { String[] PROJECTION = { Tabs.UPDATED_LOCAL, Tabs.TAB_BALANCE }; int UPDATED = 0; int BALANCE = 1; } }