Android Open Source - callmeter Add Log Activity






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) 2009-2013 Felix Bechstein
 * //from   ww  w.  j  a  v  a 2  s .c  o  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.ui;

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

import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.ContentValues;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import java.util.Calendar;
import java.util.Date;

import de.ub0r.android.callmeter.CallMeter;
import de.ub0r.android.callmeter.R;
import de.ub0r.android.callmeter.TrackingUtils;
import de.ub0r.android.callmeter.data.DataProvider;
import de.ub0r.android.callmeter.data.LogRunnerService;
import de.ub0r.android.callmeter.ui.prefs.Preferences;
import de.ub0r.android.logg0r.Log;
import de.ub0r.android.lib.Utils;

/**
 * Add a log entry.
 *
 * @author flx
 */
public final class AddLogActivity extends TrackingSherlockActivity implements OnClickListener,
        OnDateSetListener, OnTimeSetListener {

    /** Tag for output. */
    private static final String TAG = "AddLogActivity";

    /** {@link Spinner}s. */
    private Spinner spType, spDirection;
    /** {@link EditText}s. */
    private EditText etLength, etRemote;
    private TextView tvDate, tvTime;
    /** {@link CheckBox}. */
    private CheckBox cbRoamed;
    private Calendar cal;

    /**
     * {@inheritDoc}
     */
    @Override
    public void onCreate(final Bundle savedInstanceState) {
        setTheme(Preferences.getTheme(this));
        Utils.setLocale(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.logs_add);
        CallMeter.fixActionBarBackground(getSupportActionBar(), getResources(),
                R.drawable.bg_striped, R.drawable.bg_striped_split);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        setTitle(R.string.add_log);

        if (savedInstanceState == null) {
            cal = Calendar.getInstance();
        } else {
            cal = Calendar.getInstance();
            cal.setTimeInMillis(savedInstanceState.getLong("cal"));
        }

        spType = (Spinner) findViewById(R.id.type);
        spDirection = (Spinner) findViewById(R.id.direction);
        etLength = (EditText) findViewById(R.id.length);
        etRemote = (EditText) findViewById(R.id.remote);
        tvDate = (TextView) findViewById(R.id.date);
        tvTime = (TextView) findViewById(R.id.time);
        cbRoamed = (CheckBox) findViewById(R.id.roamed);
        spType.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(final AdapterView<?> parent, final View view,
                    final int position, final long id) {
                switch (position) {
                    case DataProvider.Rules.WHAT_CALL:
                        AddLogActivity.this.etLength.setHint(R.string.length_hint_call);
                        AddLogActivity.this.etLength.setVisibility(View.VISIBLE);
                        AddLogActivity.this.etRemote.setVisibility(View.VISIBLE);
                        break;
                    case DataProvider.Rules.WHAT_DATA:
                        AddLogActivity.this.etLength.setHint(R.string.length_hint_data);
                        AddLogActivity.this.etLength.setVisibility(View.VISIBLE);
                        AddLogActivity.this.etRemote.setVisibility(View.GONE);
                        break;
                    case DataProvider.Rules.WHAT_MMS:
                    case DataProvider.Rules.WHAT_SMS:
                        AddLogActivity.this.etLength.setVisibility(View.GONE);
                        AddLogActivity.this.etRemote.setVisibility(View.VISIBLE);
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onNothingSelected(final AdapterView<?> parent) {
                // nothing to do
            }
        });
        tvDate.setOnClickListener(this);
        tvTime.setOnClickListener(this);
        updateDateTime();
    }

    @Override
    protected void onSaveInstanceState(final Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putLong("cal", cal.getTimeInMillis());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean onCreateOptionsMenu(final Menu menu) {
        getSupportMenuInflater().inflate(R.menu.menu_addlogs, menu);
        return true;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean onOptionsItemSelected(final MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                TrackingUtils.sendMenu(this, "home");
                finish();
                return true;
            case R.id.item_add:
                TrackingUtils.sendMenu(this, "item_add");
                final int t = spType.getSelectedItemPosition();
                final int d = spDirection.getSelectedItemPosition();
                long l = Utils.parseLong(etLength.getText().toString(), 0L);
                final String r = etRemote.getText().toString();
                final boolean roamed = cbRoamed.isChecked();
                final ContentValues cv = new ContentValues();
                switch (t) {
                    case DataProvider.Rules.WHAT_CALL:
                        cv.put(DataProvider.Logs.TYPE, DataProvider.TYPE_CALL);
                        break;
                    case DataProvider.Rules.WHAT_DATA:
                        cv.put(DataProvider.Logs.TYPE, DataProvider.TYPE_DATA);
                        l *= CallMeter.BYTE_KB;
                        break;
                    case DataProvider.Rules.WHAT_MMS:
                        cv.put(DataProvider.Logs.TYPE, DataProvider.TYPE_MMS);
                        l = 1;
                        break;
                    case DataProvider.Rules.WHAT_SMS:
                        cv.put(DataProvider.Logs.TYPE, DataProvider.TYPE_SMS);
                        l = 1;
                        break;
                    default:
                        Log.e(TAG, "unknown type");
                        Toast.makeText(this, "unknown type", Toast.LENGTH_LONG).show();
                        return true;
                }
                cv.put(DataProvider.Logs.DIRECTION, d);
                cv.put(DataProvider.Logs.AMOUNT, l);
                cv.put(DataProvider.Logs.PLAN_ID, DataProvider.NO_ID);
                cv.put(DataProvider.Logs.RULE_ID, DataProvider.NO_ID);
                cv.put(DataProvider.Logs.REMOTE, r);
                cv.put(DataProvider.Logs.DATE, cal.getTimeInMillis());
                if (roamed) {
                    cv.put(DataProvider.Logs.ROAMED, 1);
                }
                getContentResolver().insert(DataProvider.Logs.CONTENT_URI, cv);
                LogRunnerService.update(this, null);
                if (!this.isFinishing()) {
                    finish();
                }
                TrackingUtils.sendEvent(this, "data", "logs", "add", (long) t);
                return true;
            case R.id.item_cancel:
                TrackingUtils.sendMenu(this, "item_cancel");
                if (!this.isFinishing()) {
                    finish();
                }
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onClick(final View v) {
        switch (v.getId()) {
            case R.id.date:
                DatePickerDialog dpd = new DatePickerDialog(this, this, cal.get(Calendar.YEAR),
                        cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
                dpd.show();
                break;
            case R.id.time:
                TimePickerDialog dtp = new TimePickerDialog(this, this,
                        cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE),
                        DateFormat.is24HourFormat(this));
                dtp.show();
                break;
            default:
                break;
        }
    }

    @Override
    public void onTimeSet(final TimePicker view, final int hourOfDay, final int minute) {
        cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
        cal.set(Calendar.MINUTE, minute);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 1);
        updateDateTime();
    }

    @Override
    public void onDateSet(final DatePicker view, final int year, final int monthOfYear,
            final int dayOfMonth) {
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, monthOfYear);
        cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        updateDateTime();
    }

    private void updateDateTime() {
        Date date = cal.getTime();
        tvDate.setText(DateFormat.getDateFormat(this).format(date));
        tvTime.setText(DateFormat.getTimeFormat(this).format(date));
    }

}




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