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.appwidget; import android.app.AlertDialog; import android.appwidget.AppWidgetHost; import android.appwidget.AppWidgetManager; import android.content.ContentResolver; import android.content.ContentValues; import android.content.DialogInterface; import android.content.DialogInterface.OnCancelListener; import android.content.DialogInterface.OnClickListener; import android.content.Intent; import android.database.Cursor; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.LoaderManager; import android.support.v4.content.CursorLoader; import android.support.v4.content.Loader; import android.support.v4.widget.CursorAdapter; import android.support.v4.widget.SimpleCursorAdapter; import net.niyonkuru.koodroid.App; import net.niyonkuru.koodroid.MainActivity.SubscribersQuery; import net.niyonkuru.koodroid.R; import net.niyonkuru.koodroid.provider.AccountContract.Subscribers; import net.niyonkuru.koodroid.provider.SettingsContract.Settings; import net.niyonkuru.koodroid.util.IntentUtils; /** * Configures the {@link UsageWidget} */ public class WidgetConfigureActivity extends FragmentActivity implements LoaderManager.LoaderCallbacks<Cursor> { private int mResultCode = RESULT_CANCELED; private int mAppWidgetId; private CursorAdapter mAdapter; private AlertDialog mAlertDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mAppWidgetId = getIntent().getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); final Intent resultValue = new Intent(); resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId); if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) { setResult(RESULT_CANCELED, resultValue); finish(); } // Create an empty adapter we will use to display the loaded data. mAdapter = new SimpleCursorAdapter(this, android.R.layout.select_dialog_singlechoice, null, new String[] { Subscribers.SUBSCRIBER_ID }, new int[] { android.R.id.text1 }, 0); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(R.string.choose_subscriber_title); builder.setSingleChoiceItems(mAdapter, 0, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int position) { final Cursor cursor = mAdapter.getCursor(); cursor.moveToPosition(position); ContentValues values = new ContentValues(1); values.put(Settings.SUBSCRIBER, cursor.getString(SubscribersQuery.SUBSCRIBER_ID)); ContentResolver cr = getContentResolver(); cr.insert(Settings.buildAppWidgetUri(mAppWidgetId), values); mResultCode = RESULT_OK; setResult(mResultCode, resultValue); dialog.dismiss(); finish(); } }); builder.setOnCancelListener(new OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { setResult(RESULT_CANCELED, resultValue); dialog.dismiss(); finish(); } }); builder.setNegativeButton(R.string.cancel, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); dialog.cancel(); } }); mAlertDialog = builder.create(); getSupportLoaderManager().initLoader(0, null, this); } @Override public void onPause() { super.onPause(); mAlertDialog.dismiss(); // avoid leaking window String action = getIntent().getAction(); if (mResultCode == RESULT_CANCELED && (action != null && action.equals(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE))) { /* avoid phantom widgets */ AppWidgetHost host = new AppWidgetHost(this, 1); host.deleteAppWidgetId(mAppWidgetId); } else { Intent intent = IntentUtils.createWidgetIntent(this, AppWidgetManager.ACTION_APPWIDGET_UPDATE); intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[] { mAppWidgetId }); sendBroadcast(intent); } } @Override public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) { String selection = Subscribers.SUBSCRIBER_EMAIL + "='" + App.getSettings().email() + "'"; return new CursorLoader(this, Subscribers.CONTENT_URI, SubscribersQuery.PROJECTION, selection, null, Subscribers.DEFAULT_SORT); } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor data) { mAlertDialog.show(); mAdapter.swapCursor(data); } @Override public void onLoaderReset(Loader<Cursor> loader) { mAdapter.swapCursor(null); } }