Saturday, 30 December 2017

Social Media Helper Classes

Following are helper model classes for Facebook, Twitter and Google Auth i.e. User and Response classes respectively. 

//User Model class 
import android.net.Uri;

import org.json.JSONObject;

public class User {

    public class Facebook{
 
        public String name;

        public String email;

        public String facebookID;

        public String gender;

        public String about;

        public String bio;

        public String dob;

        public String cellno;

        public String coverPicUrl;

        public String profilePic;

        public String mProfileImage;

        public JSONObject response;

    }
    public class GoogleAuth {

        public String name;

        public String email;

        public String idToken;

        public String id;

        public String dob;

        public String cellno;

        public String familyName;

        public Uri photoUrl;

        public String mProfileImage;
    }

    public class Twitter{
 
        public String name;

        public String email;

        public String description;

        public String pictureUrl;

        public String bannerUrl;

        public String language;

        public long id;

        public String cellno;

        public String dob;

        public Twitter(){ }
    }
}

//Response Model class 
import android.support.annotation.NonNull;

public class Response {

    public interface Facebook {
 
        void onFbSignInFail();

        void onFbSignInSuccess();

        void onFbProfileReceived(User.Facebook facebookUser);

        void onFBSignOut();
    }
 
    public interface GoogleAuth{

        void onGoogleAuthSignIn(User.GoogleAuth user);

        void onGoogleAuthSignInFailed();

        void onGoogleAuthSignOut(boolean isSuccess);
    }
 
    public interface Twitter {

        void onTwitterError();

        void onTwitterSignIn(@NonNull String userId, @NonNull String userName);

        void onTwitterProfileReceived(User.Twitter user);
    }
}

Google SignIn Helper


Here is the code of GoogleSignInHelper class, which you need to integrate Google account in your app.
import android.app.Activity; import android.content.Intent; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import com.google.android.gms.auth.api.Auth; import com.google.android.gms.auth.api.signin.GoogleSignInAccount; import com.google.android.gms.auth.api.signin.GoogleSignInOptions; import com.google.android.gms.auth.api.signin.GoogleSignInResult; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.api.GoogleApiClient; import com.google.android.gms.common.api.ResultCallback; import com.google.android.gms.common.api.Status; import com.spellstudio.ringin.socialauthentications.Response; import com.spellstudio.ringin.socialauthentications.User; public class GoogleSignInHelper implements GoogleApiClient.OnConnectionFailedListener { private static final int RC_SIGN_IN = 100; private FragmentActivity mContext; private Response.GoogleAuth mListener; private GoogleApiClient mGoogleApiClient; public GoogleSignInHelper(FragmentActivity context, @Nullable String serverClientId, @NonNull Response.GoogleAuth listener) { mContext = context; mListener = listener; //noinspection ConstantConditions if (listener == null) { throw new RuntimeException("GoogleAuthResponse listener cannot be null."); } //build api client buildGoogleApiClient(buildSignInOptions(serverClientId)); } private GoogleSignInOptions buildSignInOptions(@Nullable String serverClientId) { GoogleSignInOptions.Builder gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestEmail() .requestProfile() .requestId(); if (serverClientId != null) gso.requestIdToken(serverClientId); return gso.build(); } private void buildGoogleApiClient(@NonNull GoogleSignInOptions gso) { mGoogleApiClient = new GoogleApiClient.Builder(mContext) .enableAutoManage(mContext, this) .addApi(Auth.GOOGLE_SIGN_IN_API, gso) .build(); } public void performSignIn(Activity activity) { Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); activity.startActivityForResult(signInIntent, RC_SIGN_IN); } public void performSignIn(Fragment activity) { Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); activity.startActivityForResult(signInIntent, RC_SIGN_IN); } public void onActivityResult(int requestCode, int resultCode, Intent data) { // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...); if (requestCode == RC_SIGN_IN) { GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); if (result.isSuccess()) { // Signed in successfully, show authenticated UI. GoogleSignInAccount acct = result.getSignInAccount(); mListener.onGoogleAuthSignIn(parseToGoogleUser(acct)); } else { mListener.onGoogleAuthSignInFailed(); } } } private User.GoogleAuth parseToGoogleUser(GoogleSignInAccount account) { User.GoogleAuth user = new GoogleAuthUser(); user.name = account.getDisplayName(); user.familyName = account.getFamilyName(); user.idToken = account.getIdToken(); user.email = account.getEmail(); user.photoUrl = account.getPhotoUrl(); return user; } @Override public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { mListener.onGoogleAuthSignInFailed(); } public void performSignOut() { Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback( new ResultCallback<Status>() { @Override public void onResult(Status status) { mListener.onGoogleAuthSignOut(status.isSuccess()); } }); } }

Twitter Integration

Here is the code of TwitterHelper class, which you need to integrate Twitter in your app.

import android.app.Activity;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;

import com.spellstudio.ringin.socialauthentications.Response;
import com.spellstudio.ringin.socialauthentications.User;
import com.twitter.sdk.android.Twitter;
import com.twitter.sdk.android.core.Callback;
import com.twitter.sdk.android.core.Result;
import com.twitter.sdk.android.core.TwitterAuthConfig;
import com.twitter.sdk.android.core.TwitterException;
import com.twitter.sdk.android.core.TwitterSession;
import com.twitter.sdk.android.core.identity.TwitterAuthClient;
import com.twitter.sdk.android.core.models.User;

import io.fabric.sdk.android.Fabric;

//import com.twitter.sdk.android.core.Twitter;


public class TwitterHelper {

    private TwitterAuthClient mAuthClient;

    @NonNull
    private final Activity mActivity;

    @NonNull
    private final Response.Twitter mListener;

    /**
     * Public constructor. This will initialize twitter sdk.
     *
     * @param twitterApiKey     twitter api key
     * @param twitterSecreteKey twitter secrete key
     * @param response          {@link TwitterResponse} response listener.
     * @param context           instance of the caller.
     */
    public TwitterHelper(@StringRes final int twitterApiKey,
                         @StringRes final int twitterSecreteKey,
                         @NonNull Response.Twitter response,
                         @NonNull Activity context) {

        //noinspection ConstantConditions
        if (response == null) throw new IllegalArgumentException("TwitterResponse cannot be null.");

        mActivity = context;
        mListener = response;

        //initialize sdk
        TwitterAuthConfig authConfig = new TwitterAuthConfig(context.getResources().getString(twitterApiKey),
                context.getResources().getString(twitterSecreteKey));
        Fabric.with(context, new Twitter(authConfig));

        mAuthClient = new TwitterAuthClient();
    }

    /**
     * Result callback.
     */
    private Callback<TwitterSession> mCallback = new Callback<TwitterSession>() {
        @Override
        public void success(Result<TwitterSession> result) {
            TwitterSession session = result.data;
            mListener.onTwitterSignIn(session.getUserName(), session.getUserId() + " ");

            //load user data.
            getUserData();
        }

        @Override
        public void failure(TwitterException exception) {
            mListener.onTwitterError();
        }
    };

    /**
     * Perform twitter sign in. Call this method when user clicks on "Login with Twitter" button.
     */
    public void performSignIn() {
        mAuthClient.authorize(mActivity, mCallback);
    }

    /**
     * This method handles onActivityResult callbacks from fragment or activity.
     *
     * @param requestCode request code received.
     * @param resultCode  result code received.
     * @param data        Data intent.
     */
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (mAuthClient != null)
            mAuthClient.onActivityResult(requestCode, resultCode, data);
    }

    /**
     * Load twitter user profile.
     */
    private void getUserData() {

        Twitter.getApiClient().getAccountService().verifyCredentials(true, false, new Callback<User>() {
            @Override
            public void success(Result<User> userResult) {
                //parse the response
                User mUser = new User();
                User.Twitter user =  new mUser.Twitter();
                user.name = userResult.data.name;
                user.email = userResult.data.email;
                user.description = userResult.data.description;
                user.pictureUrl = userResult.data.profileImageUrl;
                user.bannerUrl = userResult.data.profileBannerUrl;
                user.language = userResult.data.lang;
                user.id = userResult.data.id;

                mListener.onTwitterProfileReceived(user);
            }

            @Override
            public void failure(TwitterException e) {
                mListener.onTwitterError();
            }
        });
    }
}

Facebook Integration

Here is the code of FacebookHelper class, which you need to integrate facebook in your app.

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;

import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;
import com.facebook.login.LoginManager;
import com.facebook.login.LoginResult;
import com.spellstudio.ringin.socialauthentications.Response;
import com.spellstudio.ringin.socialauthentications.User;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Arrays;

public class FacebookHelper {
    private Response.Facebook mListener;
    private String mFieldString;
    private CallbackManager mCallBackManager;

    public FacebookHelper(Response.Facebook responseListener,
                          String fieldString,
                          Activity context) {
        FacebookSdk.sdkInitialize(context.getApplicationContext());

        //noinspection ConstantConditions
        if (responseListener == null)
            throw new IllegalArgumentException("FacebookResponse listener cannot be null.");

        //noinspection ConstantConditions
        if (fieldString == null) throw new IllegalArgumentException("field string cannot be null.");

        mListener = responseListener;
        mFieldString = fieldString;
        mCallBackManager = CallbackManager.Factory.create();

        //get access token
        FacebookCallback<LoginResult> mCallBack = new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                mListener.onFbSignInSuccess();

                //get the user profile
                getUserProfile(loginResult);
            }

            @Override
            public void onCancel() {
                mListener.onFbSignInFail();
            }

            @Override
            public void onError(FacebookException e) {
                mListener.onFbSignInFail();
            }
        };
        LoginManager.getInstance().registerCallback(mCallBackManager, mCallBack);
    }

    /**
     * Get user facebook profile.
     *
     * @param loginResult login result with user credentials.
     */
    private void getUserProfile(LoginResult loginResult) {
        // App code
        GraphRequest request = GraphRequest.newMeRequest(
                loginResult.getAccessToken(),
                new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted(JSONObject object, GraphResponse response) {

                        Log.e("response: ", response + "");
                        try {
                            mListener.onFbProfileReceived(parseResponse(object));
                        } catch (Exception e) {
                            e.printStackTrace();

                            mListener.onFbSignInFail();
                        }
                    }
                });

        Bundle parameters = new Bundle();
        parameters.putString("fields", mFieldString);
        request.setParameters(parameters);
        request.executeAsync();
    }

    /**
     * Get the {@link CallbackManager} for managing callbacks.
     *
     * @return {@link CallbackManager}
     */

    public CallbackManager getCallbackManager() {
        return mCallBackManager;
    }

    /**
     * Parse the response received into {@link FacebookUser} object.
     *
     * @param object response received.
     * @return {@link FacebookUser} with required fields.
     * @throws JSONException
     */
    private User.Facebook parseResponse(JSONObject object) throws JSONException {
        User mUser = new User();
        User.Facebook user = new mUser.Facebook();
        user.response = object;

        if (object.has("id")) user.facebookID = object.getString("id");
        if (object.has("email")) user.email = object.getString("email");
        if (object.has("name")) user.name = object.getString("name");
        if (object.has("gender")) user.gender = object.getString("gender");
        if (object.has("about")) user.about = object.getString("about");
        if (object.has("bio")) user.bio = object.getString("bio");
        if (object.has("user_birthday")) user.dob = object.getString("user_birthday");
        if (object.has("cover"))
            user.coverPicUrl = object.getJSONObject("cover").getString("source");
        if (object.has("picture"))
            user.profilePic = object.getJSONObject("picture").getJSONObject("data").getString("url");
        return user;
    }

    /**
     * Perform facebook sign in.<p>
     * NOTE: If you are signing from the fragment than you should call {@link #performSignIn(Fragment)}.<p>
     * This method should generally call when user clicks on "Sign in with Facebook" button.
     *
     * @param activity instance of the caller activity.
     */
    public void performSignIn(Activity activity) {
        LoginManager.getInstance().logInWithReadPermissions(activity, Arrays.asList("public_profile", "user_friends", "email"));
    }

    /**
     * Perform facebook login. This method should be called when you are signing in from
     * fragment.<p>
     * This method should generally call when user clicks on "Sign in with Facebook" button.
     *
     * @param fragment caller fragment.
     */
    public void performSignIn(Fragment fragment) {
        LoginManager.getInstance().logInWithReadPermissions(fragment, Arrays.asList("public_profile", "user_friends", "email"));
    }

    /**
     * This method handles onActivityResult callbacks from fragment or activity.
     *
     * @param requestCode request code received.
     * @param resultCode  result code received.
     * @param data        Data intent.
     */
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        mCallBackManager.onActivityResult(requestCode, resultCode, data);
    }

    public void performSignOut() {
        LoginManager.getInstance().logOut();
        mListener.onFBSignOut();
    }
}

Sunday, 24 December 2017

SMS BroadcastReceiver, Reading Complete SMS


Using BroadcastReceiver, allows the system to get alert whenever a message received. Upon message reception, you can handle as per requirement. A message may be composed of multi-parts, so, you can combine it and show as a single complete message. Following is the onReceive method:

@Override
public void onReceive(Context context, Intent intent) {        
    
    // Get the data (SMS data) bound to intent
    StringBuilder text=new StringBuilder();
    
    // Retrieves a map of extended data from the intent.    
    final Bundle bundle = intent.getExtras();
    Log.d(TAG, "onReceive");
    try {

        if (bundle != null) {

            final Object[] pdusObj = (Object[]) bundle.get("pdus");

            for (int parts = 0; parts < pdusObj.length; parts++) {
                SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[parts]);
                String senderNumber = currentMessage.getDisplayOriginatingAddress();
                String message = currentMessage.getDisplayMessageBody();

                Log.i("SmsReceiver", "senderNumber: "+ senderNumber + "; message: " + message);
                text.append(message); //combine sms parts as a single message            } // end for loop

            // Show Alert            
            Toast.makeText(context, "Complete Message: " +text.toString() , Toast.LENGTH_LONG).show();
        } // bundle is null
    } catch (Exception e) {

        Log.e("SmsReceiver", "Exception smsReceiver" +e);
    }
 }

Handling Permission and Sending SMS

SMS sending and receiving is simple but due to advancements in permissions since Marshmallow version it needs special permission(s) to be requested runtime. Then user may or may not grant these permission(s). Earlier versions do not require permission(s) on app launch or at runtime.  Therefore, we need to take care of permission(s), depending on the version of the phone.

To request permission, we define method in the main activity as follow:

public class MainActivity extends AppCompatActivity {

@Overrideprotected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    requestSMSPermission();

    if(isSMSPermitted()){
         //Get the default instance of SmsManager         
         SmsManager smsManager = SmsManager.getDefault();
         String mobileNumber = "92xxxxxxxx"; //enter mobile number
         String smsBody = " Hi, this is a test SMS, Cheers!";
         short port = 6734;
         //Send a text SMS
         SmsManager.getDefault().sendTextMessage(phoneNumber, null, smsBody, null, null);
    }
}

/** * Request SMS permission runtime */private void requestSMSPermission() {

    if (ActivityCompat.shouldShowRequestPermissionRationale(this, 
            Manifest.permission.READ_SMS)) {

        //https://developer.android.com/training/permissions/requesting.html    }
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_SMS},
            SMS_PERMISSION_REQ_CODE);
  }

  //Handle Permission Result 
  @Override 
  public void onRequestPermissionsResult(int requestCode,
                                        String permissions[], int[] results) {

     switch (requestCode) {

         case SMS_PERMISSION_REQ_CODE: {

             if (results.length > 0 && results[0] == PackageManager.PERMISSION_GRANTED) {

                 // permission granted
                 Toast.makeText(MainActivity.this, "Permission Granted!", Toast.LENGTH_SHORT).show();

             } else {

                 // permission denied
                 Toast.makeText(MainActivity.this, "Permission Granted!", Toast.LENGTH_SHORT).show();
             }
             return;
           }
        }
    }
   /** * Check if SMS permission granted */
   public boolean isSMSPermitted() {

    return ContextCompat.checkSelfPermission(context, Manifest.permission.READ_SMS) == PackageManager.PERMISSION_GRANTED;
  }
}




Wednesday, 23 November 2016

Install / Uninstall App from Another App Dynamically


If you want to install a custom app dynamically from your app, then it is quite simple. For this, you need to place APK on a downloadable URL, so that it can be downloaded directly. Then you can access app url and install. Since you want to install app from your app, then you need to download the APK first and then install your custom app. It thus need to do in background. 
Here is the code.

I installed custom app on launch of my app i.e. MainActivity

//Download APK and then Install Custom App
InstallApp customApp= new InstallApp();
customApp.setContext(myContext);
customApp.execute("http://APK-URL/AppName.apk");

Following is the code to download the apk in background then install app.

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class InstallApp extends AsyncTask<String,Void,Void> {

    private Context context;
    private final String TAG = "InstallApp";
    public void setContext(Context context){

        this.context = context;
    }

    @Override
    protected Void doInBackground(String... apk_url) {

        try {

            //URL of the APK, to download
            URL download_url = new URL(apk_url[0]);
            HttpURLConnection c = (HttpURLConnection) download_url.openConnection();
            c.setRequestMethod("GET");
            c.setDoOutput(true);
            c.connect();

            //Download APK to download folder. Save APK file with CustomApp.apk
            File file = new File(Environment.getExternalStorageDirectory() + "/download/");
            file.mkdirs();
            File downloadFile= new File(file, "CustomApp.apk");
            if(downloadFile.exists()){

                downloadFile.delete();
            }

            FileOutputStream fos = new FileOutputStream(downloadFile);
            InputStream is = c.getInputStream();

            byte[] buffer = new byte[1024];
            int lenght = 0;
            while ((lenght = is.read(buffer)) != -1) {

                fos.write(buffer, 0, lenght);
            }

            fos.close();
            is.close();

            //Install App Intent
            Intent install_intent = new Intent(Intent.ACTION_VIEW);
            install_intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" + "CustomApp.apk")), "application/vnd.android.package-archive");
            install_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(install_intent);
        } catch (Exception e) {

            Log.e(TAG, e.getMessage());
        }
        return null;
    }
}

Similarly, if you want to uninstall app from another app, then here is code:

 //Uninstall app
Uri packageUri = Uri.parse("package:"+"name of app  package");
//OS >= 14 supports ACTION_UNINSTALL_PACKAGE
//OS < 14 ACTION_DELETE
//Therefore, for all OS, use the following.

Intent appUninstall= new Intent( (Build.VERSION.SDK_INT >= 14)?Intent.ACTION_UNINSTALL_PACKAGE: Intent.ACTION_DELETE, packageUri);

startActivity(appUninstall);