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.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.LinearLayout; import android.widget.TextView; import java.sql.Timestamp; import net.niyonkuru.koodroid.R; import net.niyonkuru.koodroid.provider.AccountContract.Addons; import net.niyonkuru.koodroid.provider.AccountContract.Plans; 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 {@link Plans} information. */ public class AccountFragment extends PageFragment { private static final int PLAN_TOKEN = 0x1; private static final int ADDONS_TOKEN = 0x2; private LayoutInflater mLayoutInflater; private TextView mPlanCost; private TextView mPlanName; private TextView mPlanUpdateTime; private TextView mAddonsCost; private LinearLayout mAddonsList; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View root = inflater.inflate(R.layout.account_fragment, container, false); root.findViewById(R.id.plan_title).setOnClickListener(this); root.findViewById(R.id.plan_options).setOnClickListener(this); if (isPlanEnabled()) { root.findViewById(R.id.plan_content).setVisibility(View.VISIBLE); } mPlanCost = (TextView) root.findViewById(R.id.plan_cost); mPlanName = (TextView) root.findViewById(R.id.plan_name); mPlanUpdateTime = (TextView) root.findViewById(R.id.plan_update_time); mAddonsCost = (TextView) root.findViewById(R.id.addons_total); mAddonsList = (LinearLayout) root.findViewById(R.id.addons); mLayoutInflater = inflater; return root; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); if (isPlanEnabled()) { LoaderManager loaderManager = getLoaderManager(); loaderManager.restartLoader(ADDONS_TOKEN, null, this); loaderManager.initLoader(PLAN_TOKEN, null, this); } } @Override public void onDestroyView() { super.onDestroyView(); if (isPlanEnabled()) { LoaderManager loaderManager = getLoaderManager(); loaderManager.destroyLoader(ADDONS_TOKEN); loaderManager.destroyLoader(PLAN_TOKEN); } } @Override public void onClick(final View v) { int id = v.getId(); switch (id) { case R.id.plan_options: { PopupMenuBuilder builder = new PopupMenuBuilder(mContext, v); if (isPlanEnabled()) { builder.addMenuResource(R.menu.plan_overflow_menu); builder.addMenuItemResource(R.id.disable_plan, R.string.disable); } else { builder.addMenuItemResource(R.id.enable_plan, 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_plan: { LoaderManager loaderManager = getLoaderManager(); loaderManager.restartLoader(ADDONS_TOKEN, null, this); loaderManager.restartLoader(PLAN_TOKEN, null, this); } case R.id.disable_plan: /* toggle content view */ View planContent = getView().findViewById(R.id.plan_content); AnimUtils.show(planContent); toggleService(Settings.PLAN_SERVICE, id == R.id.enable_plan); return true; case R.id.refresh_plan: sync(SyncService.PLAN); return true; } return false; } @Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { switch (id) { case PLAN_TOKEN: { final Uri uri = Plans.buildPlanUri(getSubscriber()); CursorLoader loader = new CursorLoader(mContext, uri, PlansQuery.PROJECTION, null, null, null); loader.setUpdateThrottle(DateUtils.SECOND_IN_MILLIS); return loader; } case ADDONS_TOKEN: { final Uri uri = Plans.buildAddonsUri(getSubscriber()); CursorLoader loader = new CursorLoader(mContext, uri, AddonsQuery.PROJECTION, null, null, Addons.DEFAULT_SORT); loader.setUpdateThrottle(DateUtils.SECOND_IN_MILLIS); return loader; } } return null; } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor data) { int id = loader.getId(); switch (id) { case PLAN_TOKEN: { if (!data.moveToFirst()) return; mPlanCost.setText(formatMoney(data.getString(PlansQuery.COST))); mPlanName.setText(data.getString(PlansQuery.NAME)); mPlanUpdateTime.setTag(Timestamp.valueOf(data.getString(PlansQuery.UPDATED))); break; } case ADDONS_TOKEN: { mAddonsList.removeAllViews(); double total_cost = 0.0; while (data.moveToNext()) { View addon = mLayoutInflater.inflate(R.layout.addon_list_item, null); final TextView addonName = (TextView) addon.findViewById(R.id.addon_name); addonName.setText(data.getString(AddonsQuery.NAME)); double cost = data.getDouble(AddonsQuery.COST); total_cost += cost; final TextView addonCost = (TextView) addon.findViewById(R.id.addon_cost); addonCost.setText(formatMoney(cost)); mAddonsList.addView(addon); } View addonsTitle = getView().findViewById(R.id.addons_content); if (mAddonsList.getChildCount() > 0) { mAddonsCost.setText(formatMoney(total_cost)); addonsTitle.setVisibility(View.VISIBLE); } else { /* make the list visible when there's at least one addon */ addonsTitle.setVisibility(View.GONE); } break; } } updateTimestamps(); } @Override protected void updateTimestamps() { RelativeTime t = new RelativeTime(mContext.getResources()); t.format(mPlanUpdateTime); } @Override public void onLoaderReset(Loader<Cursor> loader) { int id = loader.getId(); switch (id) { case PLAN_TOKEN: mPlanUpdateTime.setTag(null); break; } } @Override protected void sync() { if (isPlanEnabled()) { sync(SyncService.PLAN); } } private boolean isPlanEnabled() { return mSettings.serviceAllowed(getSubscriber(), Settings.PLAN_SERVICE); } /** * {@link Plans} query parameters */ private interface PlansQuery { String[] PROJECTION = { Plans.UPDATED_LOCAL, Plans.PLAN_NAME, Plans.PLAN_COST, Plans.ADDONS_COST }; int UPDATED = 0; int NAME = 1; int COST = 2; int ADDON_COST = 3; } /** * {@link Addons} query parameters */ private interface AddonsQuery { String[] PROJECTION = { Addons.ADDON_NAME, Addons.ADDON_COST }; int NAME = 0; int COST = 1; } }