Android Open Source - callmeter Logs App Widget Configure






From Project

Back to project page callmeter.

License

The source code is released under:

GNU General Public License

If you think the Android project callmeter listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2010-2013 Felix Bechstein
 * //  w ww. j a v  a 2s .co  m
 * This file is part of Call Meter 3G.
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; If not, see <http://www.gnu.org/licenses/>.
 */
package de.ub0r.android.callmeter.widget;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

import de.ub0r.android.callmeter.R;
import de.ub0r.android.callmeter.data.DataProvider;
import de.ub0r.android.callmeter.ui.TrackingSherlockActivity;
import de.ub0r.android.logg0r.Log;
import de.ub0r.android.lib.Utils;
import yuku.ambilwarna.AmbilWarnaDialog;
import yuku.ambilwarna.AmbilWarnaDialog.OnAmbilWarnaListener;

/**
 * Configure a stats widget.
 *
 * @author flx
 */
public final class LogsAppWidgetConfigure extends TrackingSherlockActivity
        implements OnClickListener,
        OnCheckedChangeListener, OnSeekBarChangeListener {

    /** Tag for logging. */
    private static final String TAG = "wdgtcfg";

    /** Widget id. */
    private int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
    /** {@link Spinner} holding the plan. */
    private Spinner spinner;

    /** {@link CheckBox}s. */
    private CheckBox cbShowCost, cbShowIcon, cbSmallWidget;
    /** {@link EditText}s. */
    private EditText etPlanTextSize, etStatsTextSize;
    /** {@link Button}s. */
    private Button btnTextColor, btnBgColor;
    /** {@link View}s. */
    private View vTextColor, vBgColor;
    /** {@link SeekBar}. */
    private SeekBar sbBgTransparency;

    /** Default text size. */
    static final float DEFAULT_TEXTSIZE = 10f;
    /** Default text color. */
    static final int DEFAULT_TEXTCOLOR = 0xffffffff;
    /** Default background color. */
    static final int DEFAULT_BGCOLOR = 0x80000000;
    /** Bit mask for colors. */
    private static final int BITMASK_COLOR = 0x00FFFFFF;
    /** Shift for transparency. */
    private static final int BITSHIFT_TRANSPARENCY = 24;

    /** Projection for {@link SimpleCursorAdapter} query. */
    private static final String[] PROJ_ADAPTER = new String[]{DataProvider.Plans.ID,
            DataProvider.Plans.NAME};

    /** Does the widget already exist? */
    private boolean isExistingWidget = false;

    /**
     * {@inheritDoc}
     */
    @Override
    public void onCreate(final Bundle savedInstanceState) {
        Utils.setLocale(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stats_appwidget_config);
        findViewById(R.id.hide_name).setVisibility(View.GONE);
        findViewById(R.id.shortname).setVisibility(View.GONE);
        findViewById(R.id.pbillp).setVisibility(View.GONE);
        ((TextView) findViewById(R.id.widget_plan_textsize_))
                .setText(R.string.widget_date_textsize);
        ((TextView) findViewById(R.id.widget_cost_textsize_))
                .setText(R.string.widget_details_textsize);
        spinner = (Spinner) findViewById(R.id.spinner);
        cbShowCost = (CheckBox) findViewById(R.id.cost);
        cbShowIcon = (CheckBox) findViewById(R.id.show_icon);
        cbSmallWidget = (CheckBox) findViewById(R.id.small_widget);
        etPlanTextSize = (EditText) findViewById(R.id.widget_plan_textsize);
        etStatsTextSize = (EditText) findViewById(R.id.widget_stats_textsize);
        btnTextColor = (Button) findViewById(R.id.textcolor);
        btnBgColor = (Button) findViewById(R.id.bgcolor);
        vTextColor = findViewById(R.id.textcolorfield);
        vBgColor = findViewById(R.id.bgcolorfield);
        sbBgTransparency = (SeekBar) findViewById(R.id.bgtransparency);
        setAdapter();
        findViewById(R.id.ok).setOnClickListener(this);
        findViewById(R.id.cancel).setOnClickListener(this);
        btnTextColor.setOnClickListener(this);
        btnBgColor.setOnClickListener(this);
        sbBgTransparency.setOnSeekBarChangeListener(this);
        setTextColor(DEFAULT_TEXTCOLOR);
        setBgColor(DEFAULT_BGCOLOR, false);
    }

    /** Set {@link SimpleCursorAdapter} for {@link Spinner}. */
    private void setAdapter() {
        final Cursor c = getContentResolver().query(DataProvider.Plans.CONTENT_URI,
                PROJ_ADAPTER, DataProvider.Plans.WHERE_REALPLANS, null, DataProvider.Plans.NAME);
        final SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                android.R.layout.simple_spinner_item, c, new String[]{DataProvider.Plans.NAME},
                new int[]{android.R.id.text1});
        final int pos = spinner.getSelectedItemPosition();
        spinner.setAdapter(adapter);
        if (pos >= 0 && pos < spinner.getCount()) {
            spinner.setSelection(pos);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void onResume() {
        super.onResume();
        Utils.setLocale(this);

        final Intent intent = getIntent();
        if (intent != null) {
            mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                    AppWidgetManager.INVALID_APPWIDGET_ID);
        }
        isExistingWidget = mAppWidgetId > 0;
        load();
    }

    /**
     * {@inheritDoc}
     */
    public void onClick(final View v) {
        switch (v.getId()) {
            case R.id.ok:
                SharedPreferences.Editor editor = PreferenceManager
                        .getDefaultSharedPreferences(this)
                        .edit();
                editor.putLong(LogsAppWidgetProvider.WIDGET_PLANID + mAppWidgetId,
                        spinner.getSelectedItemId());
                editor.putBoolean(LogsAppWidgetProvider.WIDGET_COST + mAppWidgetId,
                        cbShowCost.isChecked());
                editor.putBoolean(LogsAppWidgetProvider.WIDGET_ICON + mAppWidgetId,
                        cbShowIcon.isChecked());
                editor.putBoolean(LogsAppWidgetProvider.WIDGET_SMALL + mAppWidgetId,
                        cbSmallWidget.isChecked());
                editor.putFloat(LogsAppWidgetProvider.WIDGET_STATS_TEXTSIZE + mAppWidgetId,
                        Utils.parseFloat(etStatsTextSize.getText().toString(),
                                DEFAULT_TEXTSIZE));
                editor.putFloat(LogsAppWidgetProvider.WIDGET_PLAN_TEXTSIZE + mAppWidgetId,
                        Utils.parseFloat(etPlanTextSize.getText().toString(),
                                DEFAULT_TEXTSIZE));
                editor.putInt(LogsAppWidgetProvider.WIDGET_TEXTCOLOR + mAppWidgetId,
                        getTextColor());
                editor.putInt(LogsAppWidgetProvider.WIDGET_BGCOLOR + mAppWidgetId,
                        getBgColor());
                editor.commit();

                final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
                LogsAppWidgetProvider.updateWidget(this, appWidgetManager, mAppWidgetId);

                final Intent resultValue = new Intent();
                resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
                setResult(RESULT_OK, resultValue);
                finish();
                break;
            case R.id.cancel:
                finish();
                break;
            case R.id.textcolor:
                new AmbilWarnaDialog(this, getTextColor(), new OnAmbilWarnaListener() {
                    @Override
                    public void onOk(final AmbilWarnaDialog dialog, final int color) {
                        LogsAppWidgetConfigure.this.setTextColor(color);
                    }

                    @Override
                    public void onCancel(final AmbilWarnaDialog dialog) {
                        // nothing to do
                    }

                    public void onReset(final AmbilWarnaDialog dialog) {
                        LogsAppWidgetConfigure.this.setTextColor(DEFAULT_TEXTCOLOR);
                    }
                }).show();
                break;
            case R.id.bgcolor:
                new AmbilWarnaDialog(this, getBgColor(), new OnAmbilWarnaListener() {
                    @Override
                    public void onOk(final AmbilWarnaDialog dialog, final int color) {
                        LogsAppWidgetConfigure.this.setBgColor(color, false);
                    }

                    @Override
                    public void onCancel(final AmbilWarnaDialog dialog) {
                        // nothing to do
                    }

                    public void onReset(final AmbilWarnaDialog dialog) {
                        LogsAppWidgetConfigure.this.setBgColor(DEFAULT_BGCOLOR, false);
                    }
                }).show();
                break;
            default:
                break;
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.shortname:
                setAdapter();
                return;
            default:
                return;
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onProgressChanged(final SeekBar seekBar, final int progress,
            final boolean fromUser) {
        Log.d(TAG, "onProgressChanged(", progress, ")");
        final int tp = 255 - progress;
        int c = getBgColor();
        Log.d(TAG, "color: ", c);
        c = c & BITMASK_COLOR;
        Log.d(TAG, "color: ", c);
        Log.i(TAG, "transparency: " + Integer.toHexString(tp << BITSHIFT_TRANSPARENCY));
        c = c | tp << BITSHIFT_TRANSPARENCY;
        Log.d(TAG, "color: ", c);
        setBgColor(c, true);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onStartTrackingTouch(final SeekBar seekBar) {
        // nothing todo
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onStopTrackingTouch(final SeekBar seekBar) {
        // nothing todo
    }

    /**
     * Load widget's configuration.
     */
    private void load() {
        SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(this);
        long pid = p.getLong(LogsAppWidgetProvider.WIDGET_PLANID + mAppWidgetId, -1);
        SpinnerAdapter adapter = spinner.getAdapter();
        int l = spinner.getCount();
        for (int i = 0; i < l; i++) {
            if (adapter.getItemId(i) == pid) {
                spinner.setSelection(i);
                break;
            }
        }
        cbShowCost.setChecked(p.getBoolean(LogsAppWidgetProvider.WIDGET_COST
                + mAppWidgetId, false));
        cbShowIcon.setChecked(p.getBoolean(LogsAppWidgetProvider.WIDGET_ICON
                + mAppWidgetId, false));
        cbSmallWidget.setChecked(p.getBoolean(LogsAppWidgetProvider.WIDGET_SMALL
                + mAppWidgetId, false));
        float f = p.getFloat(LogsAppWidgetProvider.WIDGET_STATS_TEXTSIZE + mAppWidgetId, -1);
        if (f > 0f && f != DEFAULT_TEXTSIZE) {
            etStatsTextSize.setText(String.valueOf(f));
        } else {
            etStatsTextSize.setText(null);
        }
        f = p.getFloat(LogsAppWidgetProvider.WIDGET_PLAN_TEXTSIZE + mAppWidgetId, -1);
        if (f > 0f && f != DEFAULT_TEXTSIZE) {
            etPlanTextSize.setText(String.valueOf(f));
        } else {
            etPlanTextSize.setText(null);
        }
        setTextColor(p.getInt(LogsAppWidgetProvider.WIDGET_TEXTCOLOR + mAppWidgetId,
                DEFAULT_TEXTCOLOR));
        setBgColor(
                p.getInt(LogsAppWidgetProvider.WIDGET_BGCOLOR + mAppWidgetId, DEFAULT_BGCOLOR),
                false);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean onCreateOptionsMenu(final Menu menu) {
        if (isExistingWidget) {
            getSupportMenuInflater().inflate(R.menu.menu_widget, menu);
            return true;
        } else {
            return super.onCreateOptionsMenu(menu);
        }
    }

    @Override
    public boolean onOptionsItemSelected(final MenuItem item) {
        switch (item.getItemId()) {
            case R.id.item_del:
                PreferenceManager.getDefaultSharedPreferences(this).edit()
                        .remove(LogsAppWidgetProvider.WIDGET_PLANID + mAppWidgetId).commit();
                finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    /**
     * Get background color currently set.
     *
     * @return color
     */
    private int getBgColor() {
        return Long.decode(btnBgColor.getText().toString()).intValue();
    }

    /**
     * Set the background color to btnBgColor and vBgColorField.
     *
     * @param color           color to set
     * @param fromProgressBar true, if setColor is called from onProgessChanged()
     */
    private void setBgColor(final int color, final boolean fromProgressBar) {
        Log.d(TAG, "setBgColor(", color, ", ", fromProgressBar, ")");
        String hex = AmbilWarnaDialog.colorToString(color);
        btnBgColor.setText(hex);
        vBgColor.setBackgroundColor(color);
        if (!fromProgressBar) {
            int trans = color >> BITSHIFT_TRANSPARENCY;
            Log.d(TAG, "transparency: ", trans);
            if (trans < 0) {
                trans = 256 + trans;
                Log.d(TAG, "transparency: ", trans);
            }
            sbBgTransparency.setProgress(255 - trans);
        }
    }

    /**
     * Get text color currently set.
     *
     * @return color
     */
    private int getTextColor() {
        return Long.decode(btnTextColor.getText().toString()).intValue();
    }

    /**
     * Set the text color to btnTextColor and vTextColorField.
     *
     * @param color color to set
     */
    private void setTextColor(final int color) {
        Log.d(TAG, "setTextColor(", color, ")");
        String hex = AmbilWarnaDialog.colorToString(color);
        Log.d(TAG, "color: ", hex);
        while (hex.length() < 9) {
            hex = "#0" + hex.substring(1);
            Log.d(TAG, "color: ", hex);
        }
        btnTextColor.setText(hex);
        vTextColor.setBackgroundColor(color);
    }
}




Java Source Code List

android.preference.DatePreference.java
com.actionbarsherlock.BuildConfig.java
de.ub0r.android.callmeter.Ads.java
de.ub0r.android.callmeter.CallMeter.java
de.ub0r.android.callmeter.TrackingUtils.java
de.ub0r.android.callmeter.data.DataProvider.java
de.ub0r.android.callmeter.data.Device.java
de.ub0r.android.callmeter.data.ExportProvider.java
de.ub0r.android.callmeter.data.LogRunnerReceiver.java
de.ub0r.android.callmeter.data.LogRunnerService.java
de.ub0r.android.callmeter.data.NameCache.java
de.ub0r.android.callmeter.data.NameLoader.java
de.ub0r.android.callmeter.data.RuleMatcher.java
de.ub0r.android.callmeter.data.SysClassNet.java
de.ub0r.android.callmeter.ui.AboutActivity.java
de.ub0r.android.callmeter.ui.AddLogActivity.java
de.ub0r.android.callmeter.ui.AskForPlan.java
de.ub0r.android.callmeter.ui.Common.java
de.ub0r.android.callmeter.ui.HelpActivity.java
de.ub0r.android.callmeter.ui.IntroActivity.java
de.ub0r.android.callmeter.ui.LogsFragment.java
de.ub0r.android.callmeter.ui.PlansFragment.java
de.ub0r.android.callmeter.ui.Plans.java
de.ub0r.android.callmeter.ui.TrackingActivity.java
de.ub0r.android.callmeter.ui.TrackingSherlockActivity.java
de.ub0r.android.callmeter.ui.TrackingSherlockFragmentActivity.java
de.ub0r.android.callmeter.ui.TrackingSherlockPreferenceActivity.java
de.ub0r.android.callmeter.ui.prefs.BillModeListPreference.java
de.ub0r.android.callmeter.ui.prefs.CV2EditTextPreference.java
de.ub0r.android.callmeter.ui.prefs.CVBillModePreference.java
de.ub0r.android.callmeter.ui.prefs.CVCheckBoxPreference.java
de.ub0r.android.callmeter.ui.prefs.CVDatePreference.java
de.ub0r.android.callmeter.ui.prefs.CVEditTextPreference.java
de.ub0r.android.callmeter.ui.prefs.CVListPreference.java
de.ub0r.android.callmeter.ui.prefs.HourGroupEdit.java
de.ub0r.android.callmeter.ui.prefs.HourGroups.java
de.ub0r.android.callmeter.ui.prefs.NumberGroupEdit.java
de.ub0r.android.callmeter.ui.prefs.NumberGroups.java
de.ub0r.android.callmeter.ui.prefs.PlanEdit.java
de.ub0r.android.callmeter.ui.prefs.Plans.java
de.ub0r.android.callmeter.ui.prefs.PreferencesImport.java
de.ub0r.android.callmeter.ui.prefs.PreferencesPlain.java
de.ub0r.android.callmeter.ui.prefs.PreferencesRules.java
de.ub0r.android.callmeter.ui.prefs.Preferences.java
de.ub0r.android.callmeter.ui.prefs.RuleEdit.java
de.ub0r.android.callmeter.ui.prefs.Rules.java
de.ub0r.android.callmeter.ui.prefs.SimplePreferencesChild.java
de.ub0r.android.callmeter.ui.prefs.SimplePreferences.java
de.ub0r.android.callmeter.ui.prefs.UpDownPreference.java
de.ub0r.android.callmeter.ui.prefs.UpdateListener.java
de.ub0r.android.callmeter.widget.LogsAppWidgetConfigure.java
de.ub0r.android.callmeter.widget.LogsAppWidgetProvider.java
de.ub0r.android.callmeter.widget.StatsAppWidgetConfigure.java
de.ub0r.android.callmeter.widget.StatsAppWidgetProvider.java
yuku.ambilwarna.AmbilWarnaDialog.java
yuku.ambilwarna.AmbilWarnaKotak.java