Android Open Source - PrepayCredit Login Activity






From Project

Back to project page PrepayCredit.

License

The source code is released under:

GNU General Public License

If you think the Android project PrepayCredit 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

/*
 * This file is part of Prepay Credit for Android
 *//from  w ww . j a v a2s .  c o  m
 * Copyright  2014  Damien O'Reilly
 *
 * Prepay Credit for Android 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.
 * Prepay Credit for Android 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 Prepay Credit for Android.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Report bugs or new features at: https://github.com/DamienOReilly/PrepayCredit
 * Contact the author at:          damienreilly@gmail.com
 */

package damo.three.ie.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.telephony.TelephonyManager;
import android.text.ClipboardManager;
import android.view.View;
import android.widget.*;
import damo.three.ie.R;
import damo.three.ie.prepay.Constants;

/**
 * Provides a login UI to the user on first run, or if login credentials are incomplete.
 *
 * @author Damien O'Reilly
 */
public class LoginActivity extends Activity {

    private SharedPreferences sharedPreferences;
    private EditText mobileNumber;
    private EditText password;
    private TextView forgotPass;
    private TextView register;
    private TextView textViewSimIccid;
    private Button continueButton;
    private String iccid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        PreferenceManager.setDefaultValues(this, R.xml.settings, false);
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

        boolean firstrun = sharedPreferences.getBoolean("firstrun", true);
        // First run?, ask for credentials, otherwise go straight to login screen.
        if (firstrun || (sharedPreferences.getString("mobile", "").equals("") || sharedPreferences.getString
                ("password", "").equals(""))) {
            setUpLoginUI();
        } else {
            openPrepayCreditActivity();
        }
    }

    private void setUpLoginUI() {
        setContentView(R.layout.login_layout);
        mobileNumber = (EditText) findViewById(R.id.mobile_number);
        password = (EditText) findViewById(R.id.password);
        forgotPass = (TextView) findViewById(R.id.forgot_password_textview);
        register = (TextView) findViewById(R.id.register_textview);
        continueButton = (Button) findViewById(R.id.continue_button);

        LoginClickListener loginClickListener = new LoginClickListener();

        register.setOnClickListener(loginClickListener);
        forgotPass.setOnClickListener(loginClickListener);
        continueButton.setOnClickListener(loginClickListener);

        // To register or request lost password, 3 wants the last 6 digits from the SIM's ICCID. For convenience, we
        // will display it to the user. But only if ICCID is available. Non SIM tablets won't have this.
        TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        LinearLayout iccidLayout = (LinearLayout) findViewById(R.id.iccid_layout);

        iccid = tm.getSimSerialNumber();
        if (iccid != null && (iccid.length() >= 6)) {
            iccidLayout.setVisibility(View.VISIBLE);
            iccid = iccid.substring(iccid.length() - 6);
            textViewSimIccid = (TextView) findViewById(R.id.sim_iccid);
            textViewSimIccid.setText(String.format(getString(R.string.sim_iccid), iccid));
            textViewSimIccid.setOnClickListener(loginClickListener);
        }
    }

    private void showError(EditText field) {
        field.setError(getString(R.string.login_field_validation_error));
    }

    private void openUrl(String registerUrl) {
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse(registerUrl));
        startActivity(i);
    }

    private boolean fieldIsEmpty(EditText field) {
        return field.getText().toString().trim().equals("");
    }

    private void copyIccidToClipboard() {
        @SuppressWarnings("deprecation")
        ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        clipboard.setText(iccid);
        Toast.makeText(this, getString(R.string.clipboard), Toast.LENGTH_SHORT).show();
    }

    private void openPrepayCreditActivity() {
        Intent intent = new Intent(this, PrepayCreditActivity.class);
        startActivity(intent);
        finish();
    }

    private void saveCredentials() {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("mobile", mobileNumber.getText().toString().trim());
        editor.putString("password", password.getText().toString().trim());
        editor.putBoolean("firstrun", false);
        editor.commit();
    }

    private class LoginClickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            if (v == register) {
                openUrl(Constants.REGISTER_URL);
            }

            if (v == forgotPass) {
                openUrl(Constants.FORGOT_PASS_URL);
            }

            // Save credentials and open the usage activity.
            if (v == continueButton) {
                if (fieldIsEmpty(mobileNumber)) {
                    showError(mobileNumber);
                } else if (fieldIsEmpty(password)) {
                    showError(password);
                } else {
                    saveCredentials();
                    openPrepayCreditActivity();
                }
            }

            if (v == textViewSimIccid) {
                copyIccidToClipboard();
            }
        }
    }
}




Java Source Code List

damo.three.ie.PrepayCreditApplication.java
damo.three.ie.activity.AboutActivity.java
damo.three.ie.activity.InternetExpirationActivity.java
damo.three.ie.activity.LoginActivity.java
damo.three.ie.activity.PrepayCreditActivity.java
damo.three.ie.activity.SettingsActivity.java
damo.three.ie.fragment.AboutFragment.java
damo.three.ie.fragment.ChangeLogFragment.java
damo.three.ie.fragment.SettingsFragment.java
damo.three.ie.fragment.UpdateFragment.java
damo.three.ie.net.EasySSLSocketFactory.java
damo.three.ie.net.EasyX509TrustManager.java
damo.three.ie.net.ProcessRequest.java
damo.three.ie.net.ThreeHttpClient.java
damo.three.ie.net.ThreeHttpParameters.java
damo.three.ie.prepay.ConnectivityReceiver.java
damo.three.ie.prepay.Constants.java
damo.three.ie.prepay.InternetAddonAlarmManagerReceiver.java
damo.three.ie.prepay.InternetAddonExpireReceiver.java
damo.three.ie.prepay.UpdateAlarmManagerReceiver.java
damo.three.ie.prepay.UpdateAsyncTask.java
damo.three.ie.prepay.UpdateReceiver.java
damo.three.ie.prepay.UpdateService.java
damo.three.ie.prepay.UsageFetcher.java
damo.three.ie.prepayusage.BasicUsageItemExpireSorter.java
damo.three.ie.prepayusage.BasicUsageItemTypeSorter.java
damo.three.ie.prepayusage.BasicUsageItem.java
damo.three.ie.prepayusage.BasicUsageItemsGrouped.java
damo.three.ie.prepayusage.ExpireGroupType.java
damo.three.ie.prepayusage.InternetUsageRegistry.java
damo.three.ie.prepayusage.ItemFactory.java
damo.three.ie.prepayusage.UsageItem.java
damo.three.ie.prepayusage.items.Data.java
damo.three.ie.prepayusage.items.FreeCash.java
damo.three.ie.prepayusage.items.InternetAddon.java
damo.three.ie.prepayusage.items.OutOfBundle.java
damo.three.ie.prepayusage.items.SkypeCalls.java
damo.three.ie.prepayusage.items.Texts.java
damo.three.ie.prepayusage.items.Three2ThreeCalls.java
damo.three.ie.prepayusage.items.TopUp.java
damo.three.ie.prepayusage.items.WeekendVoiceMinutes.java
damo.three.ie.ui.BasicUsageLayout.java
damo.three.ie.ui.ExtendedScrollView.java
damo.three.ie.ui.OutOfBundleLayout.java
damo.three.ie.ui.SlidingTabLayout.java
damo.three.ie.ui.SlidingTabStrip.java
damo.three.ie.ui.ViewPagerAdapter.java
damo.three.ie.util.AccountException.java
damo.three.ie.util.CustomTagHandler.java
damo.three.ie.util.DateUtils.java
damo.three.ie.util.FileUtils.java
damo.three.ie.util.HtmlUtilities.java
damo.three.ie.util.JSONUtils.java
damo.three.ie.util.NumberUtils.java
damo.three.ie.util.PrepayException.java
damo.three.ie.util.UsageUtils.java