Friday, January 13, 2017

Secure your android java codes from decompile or reverse engineering

One Idea, If you implement JAVA 8 library with your android projects, then it is impossible to decompile by any tools available. I will show you how you implement that.

There is 2 types of gradle files:
1. Project's gradle.build and
2. App's gradle.build

For point. 1 you make changes like:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.3'
        // ADD THIS
        classpath 'me.tatarka:gradle-retrolambda:3.2.5'        
}

Then For point 2, there are some additions are required like:

apply plugin: 'com.android.application'
// ADD THIS
apply plugin: 'me.tatarka.retrolambda'

android {
// ADD THIS
compileOptions {
targetCompatibility 1.8
sourceCompatibility 1.8    }
}

Yupp, thats all. My friend. Your codes are secured from any type of reverse engineering.

Note: Plus point is you can use LAMBDA with your logic now. And addition with
compile 'com.annimon:stream:1.0.9'
You can use JAVA STREAM as well.. ;)

Tuesday, January 10, 2017

Set timer with interval at starting on boot, Show notification on proper event/condition

First of all, we create Class that starts at Boot

public class OnBootReceiver extends BroadcastReceiver {

 @Override public void onReceive(Context context, Intent intent) {

  AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
  Intent intent = new Intent(context, OnBootCalling.class);
  PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
  Calendar calendar = Calendar.getInstance();
  calendar.set(Calendar.HOUR_OF_DAY, Constant.alarmHour);
  calendar.set(Calendar.MINUTE, Constant.alarmMinute);

  alarmMgr.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(),
   Constant.alarmInterval, alarmIntent);

 }

and add this to your androidManifest.xml

<receiver    android:name=".OnBootReceiver"    android:enabled="true">
    <intent-filter>
        <category android:name="android.intent.category.DEFAULT" />

        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="android.intent.action.QUICKBOOT_POWERON" />
        <action android:name="android.intent.action.REBOOT" />
    </intent-filter>
</receiver>

So, everytime, On specific time, as set in Alarm, will call OnBootCalling.class

public class OnBootCalling extends WakefulBroadcastReceiver {
 @Override
 public void onReceive(Context context, Intent intent) {

  if (conditionIsSatisfied)
   ComponentName comp = new ComponentName(context.getPackageName(),
    AlarmService.class.getName());
  startWakefulService(context, (intent.setComponent(comp)));
  setResultCode(Activity.RESULT_OK);
  break;
 }
}
}
}
}

which will call AlarmService.class, which will call notification

public class AlarmService extends IntentService {

 public AlarmService() {
  super("AlarmService");
 }

 @Override
 public void onHandleIntent(Intent intent) {

  //If you want to play default sound with notification 
  Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  Ringtone ringtone = RingtoneManager.getRingtone(PlayerApp.getContext(), alarmUri);
  ringtone.play();

  Intent intent = new Intent(PlayerApp.getContext(), MainActivity.class);
  intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  PendingIntent pendingIntent = PendingIntent.getActivity(getContext(), 0, intent, 0);

  Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(PlayerApp.getContext())
   .setSmallIcon(R.drawable.ic_music)
   .setLargeIcon(BitmapFactory.decodeResource(getContext().getResources(), R.drawable.ic_music))
   .setContentTitle("Alert")
   .setTicker("Ticking Alert")
   .setStyle(new NotificationCompat.BigTextStyle().bigText("For BigView, Your Message comes here"))
   .setContentText("Your Message comes here")
   .setAutoCancel(true)
   .setSound(defaultSoundUri)
   .setContentIntent(pendingIntent);
  //                .addAction (R.drawable.ic_menu_manage,
  //                        getString(R.string.anyaction), pendingIntent);

  NotificationManager notificationManager =
   (NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE);

  notificationManager.notify(401, notificationBuilder.build());
 }
}

Note: Make sure you get proper context

Do not forget to add these lines to your Manifest

<receiver android:name=".OnBootCalling" />

<service android:name=".AlarmService" android:enabled="true" />

Also add these permissions

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

Hope, this helps you somewhere.