Java tutorial
/** * Copyright 2015 Google Inc. All Rights Reserved. * * 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 edu.scu.userinterface; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.v4.content.LocalBroadcastManager; import android.support.v7.app.AppCompatActivity; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.GoogleApiAvailability; import java.util.regex.Pattern; public class RegistrationActivity extends AppCompatActivity { private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000; private static final String TAG = "RegistrationActivity"; private BroadcastReceiver mRegistrationBroadcastReceiver; private ProgressBar mRegistrationProgressBar; private EditText emailid; private EditText password; private Button registerBtn; private boolean isReceiverRegistered; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_registration); emailid = (EditText) findViewById(R.id.emailid); password = (EditText) findViewById(R.id.password); mRegistrationProgressBar = (ProgressBar) findViewById(R.id.registrationProgressBar); mRegistrationBroadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { mRegistrationProgressBar.setVisibility(ProgressBar.GONE); SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); boolean sentToken = sharedPreferences.getBoolean(AppPreferences.SENT_TOKEN_TO_SERVER, false); if (sentToken) { Toast.makeText(RegistrationActivity.this, getString(R.string.gcm_send_message), Toast.LENGTH_LONG); Intent loginIntent = new Intent(RegistrationActivity.this, LoginActivity.class); startActivity(loginIntent); } else { Toast.makeText(RegistrationActivity.this, getString(R.string.token_error_message), Toast.LENGTH_LONG); } } }; registerBtn = (Button) findViewById(R.id.registerBtn); registerBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (validateUserInput() && checkPlayServices()) { // Start IntentService to register this application with GCM. Intent intent = new Intent(RegistrationActivity.this, RegistrationIntentService.class); intent.putExtra("email", emailid.getText().toString().trim()); intent.putExtra("password", password.getText().toString().trim()); mRegistrationProgressBar.setVisibility(ProgressBar.VISIBLE); startService(intent); } } }); // Registering BroadcastReceiver registerReceiver(); } private boolean validateUserInput() { String email = emailid.getText().toString().trim(); String pwd = password.getText().toString().trim(); Boolean valid = true; if (TextUtils.isEmpty(email) || !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) { emailid.setError("Enter a valid Email ID"); valid = false; } if (TextUtils.isEmpty(pwd) || pwd.length() < 8 || pwd.contains(" ")) { password.setError("Password must be atleast 8 characters long and cannot contain whitespace"); valid = false; } return valid; } @Override protected void onResume() { super.onResume(); registerReceiver(); } @Override protected void onPause() { LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReceiver); isReceiverRegistered = false; super.onPause(); } private void registerReceiver() { if (!isReceiverRegistered) { LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver, new IntentFilter(AppPreferences.REGISTRATION_COMPLETE)); isReceiverRegistered = true; } } /** * Check the device to make sure it has the Google Play Services APK. If * it doesn't, display a dialog that allows users to download the APK from * the Google Play Store or enable it in the device's system settings. */ private boolean checkPlayServices() { GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance(); int resultCode = apiAvailability.isGooglePlayServicesAvailable(this); if (resultCode != ConnectionResult.SUCCESS) { if (apiAvailability.isUserResolvableError(resultCode)) { apiAvailability.getErrorDialog(this, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST).show(); } else { Log.i(TAG, "This device is not supported."); finish(); } return false; } return true; } }