Tuesday, 3 March 2015

splash activity

package com.cssoft.speedlocker;

import static com.cssoft.speedlocker.CommonUtilities.DISPLAY_MESSAGE_ACTION;
import static com.cssoft.speedlocker.CommonUtilities.EXTRA_MESSAGE;
import static com.cssoft.speedlocker.CommonUtilities.SENDER_ID;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;

import com.cssoft.fonts.MyFonts;
import com.cssoft.sharedpref.GetSharedPref;
import com.cssoft.sharedpref.SetSharedPref;
import com.cssoft.speedlocker.R;
import com.google.android.gcm.GCMRegistrar;

public class SplashActivity extends Activity {

AsyncTask<Void, Void, Void> mRegisterTask;
private TextView txt_loading;

@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);

registerDevice();

/*
* new Handler().postDelayed(new Runnable() {
*
* @Override public void run() { registerDevice();
*
* } }, 5000);
*/

getUI();
startIntent();
}

private void getUI() {
txt_loading = (TextView) findViewById(R.id.loading);
txt_loading.setTypeface(MyFonts.AirStrike(this));
}

int count = 1;

private void startIntent() {

new Timer().scheduleAtFixedRate(new TimerTask() {

@Override
public void run() {

runOnUiThread(new Runnable() {
public void run() {
if (count == 1) {
txt_loading.setText("Loading.");
count = 2;
} else if (count == 2) {
txt_loading.setText("Loading..");
count = 3;
} else if (count == 3) {
txt_loading.setText("Loading...");
cancel();
}
}
});

}
}, 0, 1000);

new Handler().postDelayed(new Runnable() {

@Override
public void run() {
int id = GetSharedPref.getUserID(SplashActivity.this);
if (id > 0) {
startActivity(new Intent(SplashActivity.this,
SliderActivity.class));
overridePendingTransition(R.anim.zoomin, R.anim.fadeout);
finish();
} else {
startActivity(new Intent(SplashActivity.this,
ChooseActivity.class));
overridePendingTransition(R.anim.zoomin, R.anim.fadeout);
finish();
}

}
}, 3000);

}

public void registerDevice() {

// Make sure the device has the proper dependencies.
GCMRegistrar.checkDevice(this);

// Make sure the manifest was properly set - comment out this line
// while developing the app, then uncomment it when it's ready.
GCMRegistrar.checkManifest(this);

registerReceiver(mHandleMessageReceiver, new IntentFilter(
DISPLAY_MESSAGE_ACTION));

// Get GCM registration id
final String regId = GCMRegistrar.getRegistrationId(this);

if (!regId.isEmpty()) {
SetSharedPref.setGCMID(SplashActivity.this, regId);
Toast.makeText(getApplicationContext(), regId, Toast.LENGTH_SHORT)
.show();
}

System.out.println("id: " + regId);

// Check if regid already presents
if (regId.equals("")) {
// Registration is not present, register now with GCM
GCMRegistrar.register(this, SENDER_ID);
} else {
// Device is already registered on GCM
if (GCMRegistrar.isRegisteredOnServer(this)) {
// Skips registration.
Toast.makeText(getApplicationContext(),
"Already registered with GCM", Toast.LENGTH_LONG)
.show();
} else {
// Try to register again, but not in the UI thread.
// It's also necessary to cancel the thread onDestroy(),
// hence the use of AsyncTask instead of a raw thread.
final Context context = this;
mRegisterTask = new AsyncTask<Void, Void, Void>() {

@Override
protected Void doInBackground(Void... params) {
// Register on our server
// On server creates a new user

return null;
}

@Override
protected void onPostExecute(Void result) {
mRegisterTask = null;
}

};
mRegisterTask.execute(null, null, null);
}
}

}

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());

/**
* Take appropriate action on this message depending upon your app
* requirement For now i am just displaying it on the screen
* */

// Showing received message

Toast.makeText(getApplicationContext(),
"New Message: " + newMessage, Toast.LENGTH_LONG).show();

// Releasing wake lock
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();
}
}

Thursday, 26 February 2015

Validation

Android Layout XML file

Let us create an layout xml file with two EditText field one for entering email-id and other for password.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#F0F0F0"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="@string/lbl_register"
        android:textAllCaps="true"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#176CEC"
        android:textStyle="bold" />

    <EditText
        android:id="@+id/editText_email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#fff"
        android:ems="10"
        android:hint="@string/lbl_email_hint"
        android:inputType="textEmailAddress"
        android:padding="12dp" />

    <EditText
        android:id="@+id/editText_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="1dp"
        android:background="#fff"
        android:ems="10"
        android:hint="@string/lbl_password_hint"
        android:inputType="textPassword"
        android:padding="12dp" />

    <Button
        android:id="@+id/btn_signup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:background="#176CEC"
        android:text="@string/lbl_btn_signup"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#fff"
        android:textStyle="bold" />

</LinearLayout>

Android Layout XML file

package com.javatechig;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

public class MainActivity extends Activity {
 private EditText emailEditText;
 private EditText passEditText;

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

  emailEditText = (EditText) findViewById(R.id.editText_email);
  passEditText = (EditText) findViewById(R.id.editText_password);

  findViewById(R.id.btn_signup).setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View arg0) {

    final String email = emailEditText.getText().toString();
    if (!isValidEmail(email)) {
     emailEditText.setError("Invalid Email");
    }

    final String pass = passEditText.getText().toString();
    if (!isValidPassword(pass)) {
     passEditText.setError("Invalid Password");
    }

   }
  });
 }

 // validating email id
 private boolean isValidEmail(String email) {
  String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
    + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

  Pattern pattern = Pattern.compile(EMAIL_PATTERN);
  Matcher matcher = pattern.matcher(email);
  return matcher.matches();
 }

 // validating password with retype password
 private boolean isValidPassword(String pass) {
  if (pass != null && pass.length() > 6) {
   return true;
  }
  return false;
 }
}

Wednesday, 25 February 2015

Notification tab of speed lock

package com.cssoft.speedlocker;

import com.cssoft.fonts.MyFonts;
import com.cssoft.validation.Validations;
import com.cssoft.webservices.Login_User;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.style.TtsSpan.TextBuilder;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends Activity {

private TextView txt_username, txt_password, txt_forgot;
private EditText edit_username, edit_password;
private Button btn_login;
private Validations valid;

@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);

valid = new Validations(LoginActivity.this);

getUI();
setFonts();
clickEvents();

}

private void getUI() {
txt_username = (TextView) findViewById(R.id.txt_login_username);
txt_password = (TextView) findViewById(R.id.txt_login_password);
txt_forgot = (TextView) findViewById(R.id.txt_login_forgot);
edit_username = (EditText) findViewById(R.id.edit_login_username);
edit_password = (EditText) findViewById(R.id.edit_login_password);
btn_login = (Button) findViewById(R.id.btn_login);
}

private void setFonts() {
txt_username.setTypeface(MyFonts.RobotoMedium(LoginActivity.this));
txt_password.setTypeface(MyFonts.RobotoMedium(LoginActivity.this));
txt_forgot.setTypeface(MyFonts.RobotoMedium(LoginActivity.this));
edit_username.setTypeface(MyFonts.RobotoMedium(LoginActivity.this));
edit_password.setTypeface(MyFonts.RobotoMedium(LoginActivity.this));
btn_login.setTypeface(MyFonts.RobotoMedium(LoginActivity.this));
}

private void clickEvents() {

txt_forgot.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
startActivity(new Intent(LoginActivity.this,
ForgotActivity.class));
overridePendingTransition(R.anim.zoomin, R.anim.fadeout);

}
});

btn_login.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
String username = edit_username.getText().toString().trim();
String password = edit_password.getText().toString().trim();
if (!valid.isEmpty(username)) {
Toast.makeText(getApplicationContext(), "Enter Username",
Toast.LENGTH_SHORT).show();
} else if (!valid.isEmpty(password)) {
Toast.makeText(getApplicationContext(), "Enter Password",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "OK",
Toast.LENGTH_SHORT).show();

new Login_User(LoginActivity.this).execute(username,
password);

}

}
});

}
}

Monday, 23 February 2015

Web Services

Web service is a method of communication between two electronic devices over a network. It is a software function provided at a network address over the Web with the service always on as in the concept of utility computing. The W3C defines a Web service generally as:-
a software system designed to support interoperable machine-to-machine interaction over a network
Most Web services do not adopt this complex architecture.[citation needed] This article describes it in more detail.
The W3C also states:
We can identify two major classes of Web services:
  • REST-compliant Web services, in which the primary purpose of the service is to manipulate representations of Web resources using a uniform set of statelessoperations.
  • Arbitrary Web services, in which the service may expose an arbitrary set of operations

Monday, 16 February 2015

Progress bar

The following code example shows how a progress bar can be used from a worker thread to update the user interface to notify the user of progress:
 public class MyActivity extends Activity {
     private static final int PROGRESS = 0x1;

     private ProgressBar mProgress;
     private int mProgressStatus = 0;

     private Handler mHandler = new Handler();

     protected void onCreate(Bundle icicle) {
         super.onCreate(icicle);

         setContentView(R.layout.progressbar_activity);

         mProgress = (ProgressBar) findViewById(R.id.progress_bar);

         // Start lengthy operation in a background thread
         new Thread(new Runnable() {
             public void run() {
                 while (mProgressStatus < 100) {
                     mProgressStatus = doWork();

                     // Update the progress bar
                     mHandler.post(new Runnable() {
                         public void run() {
                             mProgress.setProgress(mProgressStatus);
                         }
                     });
                 }
             }
         }).start();
     }
 }
To add a progress bar to a layout file, you can use the <ProgressBar> element. By default, the progress bar is a spinning wheel (an indeterminate indicator). To change to a horizontal progress bar, apply the Widget.ProgressBar.Horizontal style, like so:
<ProgressBar
     style="@android:style/Widget.ProgressBar.Horizontal"
     ... />

Friday, 13 February 2015

time picker

In order to use TimePicker class, you have to first define the TimePicker component in your activity.xml. It is define as below −
<TimePicker
   android:id="@+id/timePicker1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content" />


After that you have to create an object of TimePicker class and get a reference of the above defined xml component. Its syntax is given below.
import android.widget.TimePicker;
private TimePicker timePicker1;
timePicker1 = (TimePicker) findViewById(R.id.timePicker1);

In order to get the time selected by the user on the screen, you will use getCurrentHour() and getCurrentMinute() method of the TimePicker Class. Their syntax is given below.
int hour = timePicker1.getCurrentHour();
int min = timePicker1.getCurrentMinute();