Android Open Source - tpf873 Main Activity






From Project

Back to project page tpf873.

License

The source code is released under:

Apache License

If you think the Android project tpf873 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 2014 Jacques Supcik/*from  www .  j av  a  2  s  . co m*/
 *
 * 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 ch.supcik.tpf873.app;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.os.Vibrator;


public class MainActivity extends Activity {

    private Vibrator mVibrator;
    private SmsManager mSmsManager;

    private AlertDialog dialog;
    private String mCurrentZone;
    private String mSmsMessage;

    private final String mDestination = "873";
    private final String[] mPrices = new String[] {
        "2.70", "2.00", "8.80", "5.70"  // 10/30, 10R/30R, J10/J30, J10R/J30R
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // load settings
        SharedPreferences settings = getPreferences(MODE_PRIVATE);
        mCurrentZone = settings.getString("zone", "10");

        mVibrator = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);
        mSmsManager = SmsManager.getDefault();

        setButtonsText();

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.confirmation_title);
        builder.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
            mSmsManager.sendTextMessage(mDestination, null, mSmsMessage, null, null);
            }
        });
        builder.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                // User cancelled the dialog
            }
        });
        dialog = builder.create();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_zone10) {
            mCurrentZone = "10";
            setButtonsText();
            return true;
        }
        if (id == R.id.action_zone30) {
            mCurrentZone = "30";
            setButtonsText();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onStop(){
        super.onStop();
        // Save settings
        SharedPreferences settings = getPreferences(MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString("zone", mCurrentZone);
        editor.commit();
    }

    private void setButtonsText() {
        final Button bt = (Button) findViewById(R.id.button);
        final Button btr = (Button) findViewById(R.id.buttonR);
        final Button btj = (Button) findViewById(R.id.buttonJ);
        final Button btjr = (Button) findViewById(R.id.buttonJR);

        bt.setText(String.format("%s", mCurrentZone));
        btr.setText(String.format("%sR", mCurrentZone));
        btj.setText(String.format("J%s", mCurrentZone));
        btjr.setText(String.format("J%sR", mCurrentZone));
    }

    public void displayConfirmDialog(View view) {
        int pressedButton = view.getId();
        mVibrator.vibrate(50);

        String ticketName;
        String ticketPrice;

        if (pressedButton == R.id.button) {
            ticketName = String.format(getString(R.string.a_ticket));
            ticketPrice = mPrices[0];
            mSmsMessage = String.format("%s", mCurrentZone);
        } else if (pressedButton == R.id.buttonR) {
            ticketName = String.format(getString(R.string.a_reduced_ticket));
            ticketPrice = mPrices[1];
            mSmsMessage = String.format("%sR", mCurrentZone);
        } else if (pressedButton == R.id.buttonJ) {
            ticketName = String.format(getString(R.string.a_daily_card));
            ticketPrice = mPrices[2];
            mSmsMessage = String.format("J%s", mCurrentZone);
        } else  {
            ticketName = String.format(getString(R.string.a_reduced_daily_card));
            ticketPrice = mPrices[3];
            mSmsMessage = String.format("J%sR", mCurrentZone);
        }

        dialog.setMessage(String.format(
            getString(R.string.confirmation_text),
            ticketName, mCurrentZone, mSmsMessage, ticketPrice)
        );

        dialog.show();
    }

}




Java Source Code List

ch.supcik.tpf873.app.MainActivity.java