Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.app;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.content.SharedPreferences;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.provider.Settings;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- import com.lifecarnival.app.utils.Connectivity;
- import com.lifecarnival.app.utils.ValidationClass;
- import com.lifecarnival.app.utils.WakeLocker;
- import static com.lifecarnival.app.CommonUtilities.SENDER_ID;
- import static com.lifecarnival.app.CommonUtilities.SERVER_URL;
- import static com.lifecarnival.app.CommonUtilities.DISPLAY_MESSAGE_ACTION;
- import static com.lifecarnival.app.CommonUtilities.EXTRA_MESSAGE;
- import com.google.android.gcm.GCMRegistrar;
- import com.google.android.gms.common.ConnectionResult;
- import com.google.android.gms.common.GooglePlayServicesUtil;
- public class RegisterActivity extends Activity {
- // UI elements
- EditText txtName;
- EditText txtEmail;
- EditText txtPass;
- EditText txtUserName;
- Connectivity netConnect;
- Button btnRegister;
- public static final String PREFS_NAME = "Registration";
- private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
- private static final String TAG = "GCMIntentService";
- AsyncTask<Void, Void, Void> mRegisterTask;
- static String name,email,pass,username;
- SharedPreferences settings;;
- SharedPreferences.Editor prefEditor;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.gcmregistration);
- settings = getSharedPreferences(PREFS_NAME, 0);
- prefEditor = settings.edit();
- netConnect = new Connectivity(getApplicationContext());
- // Check if GCM configuration is set
- if (SERVER_URL == null || SENDER_ID == null || SERVER_URL.length() == 0
- || SENDER_ID.length() == 0) {
- // GCM sernder id / server url is missing
- showAlert("Server Configuration Error", "Please contact application developer.");
- // stop executing code by return
- return;
- }
- txtName = (EditText) findViewById(R.id.txtName);
- txtEmail = (EditText) findViewById(R.id.txtEmail);
- txtPass = (EditText) findViewById(R.id.txtPassword);
- txtUserName = (EditText) findViewById(R.id.txtuserName);
- btnRegister = (Button) findViewById(R.id.btnRegister);
- btnRegister.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View arg0) {
- // Read EditText dat
- name = txtName.getText().toString();
- email = txtEmail.getText().toString();
- pass = txtPass.getText().toString();
- username = txtUserName.getText().toString();
- // Check if user filled the form
- if(ValidationClass.validateName(username) && ValidationClass.validateName(name) && ValidationClass.validateEmail(email) && ValidationClass.validatePassword(pass)){
- // Check if Internet present
- if (!netConnect.haveNetworkConnection()) {
- // Internet Connection is not present
- showAlert("Internet Connection Error", "Please connect to internet before registration.");
- // stop executing code by return
- return;
- }
- else
- {
- processRegistration();
- }
- }
- else{
- // user didn't fill that data
- // ask him to fill the form
- showAlert("Registration Error!", "Please enter your details correctly");
- }
- }
- });
- }
- void processRegistration()
- {
- /*
- if(!checkPlayServices())
- {
- showAlert("Google Play Service","Your Google Play Service APK is incompatible. Please download the latest APK from the playstore.");
- finish();
- }
- */
- // Make sure the device has the proper dependencies.
- GCMRegistrar.checkDevice(this);
- GCMRegistrar.checkManifest(this);
- registerReceiver(mHandleMessageReceiver, new IntentFilter(
- DISPLAY_MESSAGE_ACTION));
- // Get GCM registration id
- final String regId = GCMRegistrar.getRegistrationId(this);
- // Check if regid already presents
- if (regId.equals("")) {
- // Registration is not present, register now with GCM
- System.out.println("Empty string reg id");
- GCMRegistrar.register(this, SENDER_ID);
- }
- else {
- // Device is already registered on GCM
- if (GCMRegistrar.isRegisteredOnServer(this)) {
- // Skips registration.
- System.out.println("some string reg id");
- Toast.makeText(getApplicationContext(), "Already registered with GCM", Toast.LENGTH_LONG).show();
- }
- else {
- final Context context = this;
- System.out.println("async reg will execute");
- mRegisterTask = new AsyncTask<Void, Void, Void>() {
- @Override
- protected Void doInBackground(Void... params) {
- // Register on our server
- // On server creates a new user
- ServerUtilities.register(context, name, username, email, pass, regId);
- return null;
- }
- @Override
- protected void onPostExecute(Void result) {
- mRegisterTask = null;
- }
- };
- mRegisterTask.execute(null, null, null);
- }
- }
- prefEditor.putString("registered", "yes");
- prefEditor.putString("user_name", name);
- prefEditor.putString("user_email", email);
- prefEditor.commit();
- showAlert("Success!", "Congratulations! You are now registered to Life Carnival.\n Note: You don't need to sign in for this app.");
- }
- /*
- private void checkGoogleAccountSync()
- {
- AccountManager am = AccountManager.get(this);
- boolean syncEnabled = false;
- Account[] accounts = am.getAccountsByType("com.google");
- if(accounts.length>0)
- {
- syncEnabled = ContentResolver.getSyncAutomatically(accounts[0], ContactsContract.AUTHORITY);
- }
- Log.d("ACCOUNT SYNC", "syncStatusofaccount"+syncEnabled);
- if(!syncEnabled){
- AlertDialog.Builder showAlert = new AlertDialog.Builder(RegisterActivity.this);
- showAlert.setTitle("Google Account Sync Required");
- showAlert.setMessage("Your device is not synced with a Google account(e.g. gmail account). " +
- " Please go to Settings->Accounts&Sync or you can click Sync Device now.");
- showAlert.setPositiveButton("Sync Device",new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog,int whichButton) {
- // TODO Auto-generated method stub
- Intent intent = new Intent(Settings.ACTION_SYNC_SETTINGS);
- startActivity(intent);
- }
- });
- showAlert.setNegativeButton("No", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog,int whichButton) {
- // TODO Auto-generated method stub
- finish();
- Intent intent = new Intent(getApplicationContext(),MainActivity.class);
- startActivity(intent);
- }
- });
- showAlert.show();
- }
- }
- */
- public void showAlert(String title, String message)
- {
- AlertDialog.Builder showAlert = new AlertDialog.Builder(RegisterActivity.this);
- showAlert.setTitle(title);
- showAlert.setMessage(message);
- showAlert.setPositiveButton("OK",new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog,int whichButton) {
- // TODO Auto-generated method stub
- }
- });
- showAlert.show();
- Intent intent = new Intent(Settings.ACTION_SYNC_SETTINGS);
- startActivity(intent);
- }
- private boolean checkPlayServices() {
- int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
- if (resultCode != ConnectionResult.SUCCESS) {
- if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
- GooglePlayServicesUtil.getErrorDialog(resultCode, this,
- PLAY_SERVICES_RESOLUTION_REQUEST).show();
- } else {
- Log.i(TAG, "This device is not supported.");
- finish();
- }
- return false;
- }
- return true;
- }
- private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- String newMessage = intent.getExtras().getString(EXTRA_MESSAGE);
- // Waking up mobile if it is sleeping
- WakeLocker.acquire(getApplicationContext());
- WakeLocker.release();
- }
- };
- @Override
- protected void onDestroy() {
- if (mRegisterTask != null) {
- mRegisterTask.cancel(true);
- }
- try {
- unregisterReceiver(mHandleMessageReceiver);
- GCMRegistrar.onDestroy(this);
- } catch (Exception e) {
- Log.e("UnRegister Receiver Error", "> " + e.getMessage());
- }
- super.onDestroy();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement