Advertisement
Guest User

Register.java

a guest
Oct 23rd, 2013
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.62 KB | None | 0 0
  1. package com.example.app;
  2.  
  3. import android.app.Activity;
  4. import android.app.AlertDialog;
  5. import android.content.BroadcastReceiver;
  6. import android.content.Context;
  7. import android.content.DialogInterface;
  8. import android.content.Intent;
  9. import android.content.IntentFilter;
  10. import android.content.SharedPreferences;
  11. import android.os.AsyncTask;
  12. import android.os.Bundle;
  13. import android.provider.Settings;
  14. import android.util.Log;
  15. import android.view.View;
  16. import android.widget.Button;
  17. import android.widget.EditText;
  18. import android.widget.Toast;
  19.  
  20. import com.lifecarnival.app.utils.Connectivity;
  21. import com.lifecarnival.app.utils.ValidationClass;
  22. import com.lifecarnival.app.utils.WakeLocker;
  23.  
  24. import static com.lifecarnival.app.CommonUtilities.SENDER_ID;
  25. import static com.lifecarnival.app.CommonUtilities.SERVER_URL;
  26. import static com.lifecarnival.app.CommonUtilities.DISPLAY_MESSAGE_ACTION;
  27. import static com.lifecarnival.app.CommonUtilities.EXTRA_MESSAGE;
  28. import com.google.android.gcm.GCMRegistrar;
  29. import com.google.android.gms.common.ConnectionResult;
  30. import com.google.android.gms.common.GooglePlayServicesUtil;
  31.  
  32. public class RegisterActivity extends Activity {
  33.    
  34.     // UI elements
  35.     EditText txtName;
  36.     EditText txtEmail;
  37.     EditText txtPass;
  38.     EditText txtUserName;
  39.     Connectivity netConnect;
  40.     Button btnRegister;
  41.     public static final String PREFS_NAME = "Registration";
  42.     private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
  43.     private static final String TAG = "GCMIntentService";
  44.     AsyncTask<Void, Void, Void> mRegisterTask;
  45.     static String name,email,pass,username;
  46.     SharedPreferences settings;;
  47.     SharedPreferences.Editor prefEditor;
  48.  
  49.     @Override
  50.     public void onCreate(Bundle savedInstanceState) {
  51.         super.onCreate(savedInstanceState);
  52.         setContentView(R.layout.gcmregistration);
  53.        
  54.         settings = getSharedPreferences(PREFS_NAME, 0);
  55.         prefEditor = settings.edit();
  56.        
  57.         netConnect = new Connectivity(getApplicationContext());
  58.  
  59.         // Check if GCM configuration is set
  60.         if (SERVER_URL == null || SENDER_ID == null || SERVER_URL.length() == 0
  61.                 || SENDER_ID.length() == 0) {
  62.             // GCM sernder id / server url is missing
  63.             showAlert("Server Configuration Error", "Please contact application developer.");
  64.             // stop executing code by return
  65.              return;
  66.         }
  67.        
  68.         txtName = (EditText) findViewById(R.id.txtName);
  69.         txtEmail = (EditText) findViewById(R.id.txtEmail);
  70.         txtPass = (EditText) findViewById(R.id.txtPassword);
  71.         txtUserName = (EditText) findViewById(R.id.txtuserName);
  72.         btnRegister = (Button) findViewById(R.id.btnRegister);
  73.        
  74.        
  75.         btnRegister.setOnClickListener(new View.OnClickListener() {
  76.            
  77.             @Override
  78.             public void onClick(View arg0) {
  79.                 // Read EditText dat
  80.                 name = txtName.getText().toString();
  81.                 email = txtEmail.getText().toString();
  82.                 pass = txtPass.getText().toString();
  83.                 username = txtUserName.getText().toString();
  84.                 // Check if user filled the form
  85.                 if(ValidationClass.validateName(username) && ValidationClass.validateName(name) && ValidationClass.validateEmail(email) && ValidationClass.validatePassword(pass)){
  86.                     // Check if Internet present
  87.                     if (!netConnect.haveNetworkConnection()) {
  88.                         // Internet Connection is not present
  89.                         showAlert("Internet Connection Error", "Please connect to internet before registration.");
  90.                         // stop executing code by return
  91.                         return;
  92.                     }
  93.                     else
  94.                     {
  95.                         processRegistration();
  96.                     }
  97.                 }
  98.                 else{
  99.                     // user didn't fill that data
  100.                     // ask him to fill the form
  101.                     showAlert("Registration Error!", "Please enter your details correctly");
  102.                 }
  103.             }
  104.         });
  105.     }
  106.    
  107.     void processRegistration()
  108.     {
  109.         /*
  110.         if(!checkPlayServices())
  111.         {
  112.             showAlert("Google Play Service","Your Google Play Service APK is incompatible. Please download the latest APK from the playstore.");
  113.             finish();
  114.         }
  115.         */
  116.         // Make sure the device has the proper dependencies.
  117.         GCMRegistrar.checkDevice(this);
  118.         GCMRegistrar.checkManifest(this);
  119.         registerReceiver(mHandleMessageReceiver, new IntentFilter(
  120.                 DISPLAY_MESSAGE_ACTION));
  121.         // Get GCM registration id
  122.         final String regId = GCMRegistrar.getRegistrationId(this);
  123.  
  124.         // Check if regid already presents
  125.         if (regId.equals("")) {
  126.             // Registration is not present, register now with GCM  
  127.             System.out.println("Empty string reg id");
  128.             GCMRegistrar.register(this, SENDER_ID);
  129.         }
  130.         else {
  131.             // Device is already registered on GCM
  132.             if (GCMRegistrar.isRegisteredOnServer(this)) {
  133.                 // Skips registration.
  134.                 System.out.println("some string reg id");
  135.                 Toast.makeText(getApplicationContext(), "Already registered with GCM", Toast.LENGTH_LONG).show();
  136.             }
  137.             else {
  138.                 final Context context = this;
  139.                 System.out.println("async reg will execute");
  140.                 mRegisterTask = new AsyncTask<Void, Void, Void>() {
  141.  
  142.                     @Override
  143.                     protected Void doInBackground(Void... params) {
  144.                         // Register on our server
  145.                         // On server creates a new user
  146.                         ServerUtilities.register(context, name, username, email, pass, regId);
  147.                         return null;
  148.                     }
  149.  
  150.                     @Override
  151.                     protected void onPostExecute(Void result) {
  152.                         mRegisterTask = null;
  153.                     }
  154.  
  155.                 };
  156.                 mRegisterTask.execute(null, null, null);
  157.             }
  158.         }
  159.         prefEditor.putString("registered", "yes");
  160.         prefEditor.putString("user_name", name);
  161.         prefEditor.putString("user_email", email);
  162.         prefEditor.commit();
  163.         showAlert("Success!", "Congratulations! You are now registered to Life Carnival.\n Note: You don't need to sign in for this app.");
  164.     }
  165.     /*
  166.     private void checkGoogleAccountSync()
  167.     {
  168.         AccountManager am = AccountManager.get(this);
  169.         boolean syncEnabled = false;
  170.         Account[] accounts = am.getAccountsByType("com.google");
  171.         if(accounts.length>0)
  172.         {
  173.             syncEnabled = ContentResolver.getSyncAutomatically(accounts[0], ContactsContract.AUTHORITY);
  174.         }
  175.         Log.d("ACCOUNT SYNC", "syncStatusofaccount"+syncEnabled);
  176.  
  177.         if(!syncEnabled){
  178.            
  179.             AlertDialog.Builder showAlert = new AlertDialog.Builder(RegisterActivity.this);
  180.             showAlert.setTitle("Google Account Sync Required");
  181.             showAlert.setMessage("Your device is not synced with a Google account(e.g. gmail account). " +
  182.                     " Please go to Settings->Accounts&Sync or you can click Sync Device now.");
  183.             showAlert.setPositiveButton("Sync Device",new DialogInterface.OnClickListener() {
  184.                        
  185.                 public void onClick(DialogInterface dialog,int whichButton) {
  186.                     // TODO Auto-generated method stub
  187.                     Intent intent = new Intent(Settings.ACTION_SYNC_SETTINGS);
  188.                     startActivity(intent);
  189.                 }
  190.             });
  191.             showAlert.setNegativeButton("No", new DialogInterface.OnClickListener() {
  192.                
  193.                 public void onClick(DialogInterface dialog,int whichButton) {
  194.                     // TODO Auto-generated method stub
  195.                     finish();
  196.                    
  197.                     Intent intent = new Intent(getApplicationContext(),MainActivity.class);
  198.                     startActivity(intent);
  199.                    
  200.                 }
  201.             });
  202.             showAlert.show();
  203.            
  204.            
  205.         }
  206.     }
  207.     */
  208.     public void showAlert(String title, String message)
  209.     {
  210.         AlertDialog.Builder showAlert = new AlertDialog.Builder(RegisterActivity.this);
  211.         showAlert.setTitle(title);
  212.         showAlert.setMessage(message);
  213.         showAlert.setPositiveButton("OK",new DialogInterface.OnClickListener() {
  214.                    
  215.             public void onClick(DialogInterface dialog,int whichButton) {
  216.                 // TODO Auto-generated method stub
  217.             }
  218.         });
  219.         showAlert.show();
  220.         Intent intent = new Intent(Settings.ACTION_SYNC_SETTINGS);
  221.         startActivity(intent);
  222.     }
  223.    
  224.     private boolean checkPlayServices() {
  225.         int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
  226.         if (resultCode != ConnectionResult.SUCCESS) {
  227.             if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
  228.                 GooglePlayServicesUtil.getErrorDialog(resultCode, this,
  229.                         PLAY_SERVICES_RESOLUTION_REQUEST).show();
  230.             } else {
  231.                 Log.i(TAG, "This device is not supported.");
  232.                 finish();
  233.             }
  234.             return false;
  235.         }
  236.         return true;
  237.     }
  238.    
  239.     private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
  240.         @Override
  241.         public void onReceive(Context context, Intent intent) {
  242.             String newMessage = intent.getExtras().getString(EXTRA_MESSAGE);
  243.             // Waking up mobile if it is sleeping
  244.             WakeLocker.acquire(getApplicationContext());
  245.            
  246.             WakeLocker.release();
  247.         }
  248.     };
  249.    
  250.     @Override
  251.     protected void onDestroy() {
  252.         if (mRegisterTask != null) {
  253.             mRegisterTask.cancel(true);
  254.         }
  255.         try {
  256.             unregisterReceiver(mHandleMessageReceiver);
  257.             GCMRegistrar.onDestroy(this);
  258.         } catch (Exception e) {
  259.             Log.e("UnRegister Receiver Error", "> " + e.getMessage());
  260.         }
  261.         super.onDestroy();
  262.     }
  263.  
  264. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement