Wednesday, 30 April 2014

Timeouting onPause for activity for more operations

Here is some code how to timeout some time for onPause to i.e
  • show dialog (activity behaving like dialog) 
    • when we click on notification 
      • only when any of our activity is displaying  
This example is quite hard to describe in short but there is explanation how to make custom OnClickListener for notification in my one of the latest posts. And by using that code with this post you can achieve this magic trick..

Here will be some help how to open second activity with custom dialog. It makes no sense for normal displaying some data, but for notifications it is very helpful. This is just silly example, how to make it and make it clear.
 So what I did was:

In Application class

 public class MyApplication extends Application {  
      
      //... application methods  

      //declaring flag to know if activity is running  
      public boolean isRunning;  
      public long timeout;  
 }  

For every activity I added to onResume and onPause methods, flag - it will describing if activity is running.

 @Override  
 protected void onResume() {  
   super.onResume();  
   application.isRunning = true;  
 }  

 @Override  
 protected void onPause() {  
   super.onPause();  
   application.isRunning = false;//setting field in Application  
   application.timeout=System.currentTimeMillis();  
 }  

Also there was declared second field describing what is the current time - timeout - in Application class

Let's say we want show this "dialog" when I click the second activity, so in onCreate this second activity you need add something like th

 if (application.isRunning || timeoutLessThan(application, 300)) {  
   //showing dialog  
 } else {  
   //nothing to do  
 }  

 private boolean timeoutLessThan(NHApplication application, int value) {  
      long abs = Math.abs(application.timeout - System.currentTimeMillis());  
      Log.d(TAG, "diff timeout " + abs);  
      return abs < value;  
 }  


This timeout will help when you want do some fast action in 300ms before pausing the first activity.

Monday, 28 April 2014

Android - Make layout clickable with its children

To make your layout clickable with its children you need add this option for every child

 android:clickable="false"   

Of course I am talking about only clickable state, not drawable state which is the another story.

 <LinearLayout  
      android:id="@+id/parent"  
      android:layout_width="match_parent"  
      android:layout_height="wrap_content"  
      android:orientation="horizontal">  
      <ImageButton  
           android:clickable="false"  
           android:layout_width="40dp"  
           android:layout_height="40dp"  
           android:text="Cinema"  
           android:textSize="13sp"  
           android:layout_marginRight="10dp" />  
      <Button  
           android:clickable="false"  
           android:layout_width="match_parent"  
           android:layout_height="40dp"  
           android:text="Check in"  
           android:textSize="13sp" />  
 </LinearLayout>  

Sunday, 27 April 2014

Libgdx - parse Color from String hex to 3x float -> new Color( float, float, float, float)

Here is short code. Very lazy but it works anyway. I ommited transparency.
This code converts String hext to three floats with alpha level 1

 public static Color parseColor(String hex) {  
      String s1 = hex.substring(0, 2);  
      int v1 = Integer.parseInt(s1, 16);  
      float f1 = (float) v1 / 255f;  
      String s2 = hex.substring(2, 4);  
      int v2 = Integer.parseInt(s2, 16);  
      float f2 = (float) v2 / 255f;  
      String s3 = hex.substring(4, 6);  
      int v3 = Integer.parseInt(s3, 16);  
      float f3 = (float) v3 / 255f;  
      return new Color(f1, f2, f3, 1);  
 }  

Example:

 interface Pallete {  
      public static Color blue = parseColor("33B5E5");  
      public static Color violet = parseColor("AA66CC");  
      public static Color green = parseColor("99CC00");  
      public static Color orange = parseColor("FFBB33");  
      public static Color red = parseColor("FF4444");  
      public static Color yellow = parseColor("FFDE00");  
      public static Color brown = parseColor("593E1A");  
      public static Color gray = parseColor("BDD8DA");  
      //  
      public static Color white = parseColor("FFFFFF");  
      public static Color black = parseColor("000000");  
 }  

Wednesday, 23 April 2014

Setting onClick listener for GCM notification

Firstly, there is no such thing like setOnClickListerner for GCM notification but you can combine your own intent with custom multiple actions.

Normally, you creates intent which is goint to pendingIntent which creates notification - and.. this intent is like nothing else simple activity.

This looks something like that:
  1. You set own action for intent, i.e create sms with custom text or call for custom number
  2. Then you create PendingIntent which is taking as one of few arguments our intent
  3. Next step is use Notification.Builder and set its behaviour for notification. You also add to it your pendingIntent(point 2) which will call action from intent (point 1)
  4. As a last step you build it and add to notify
So as you see, there is not place for such thing like onClick, but hey you can create own activity which will call in onCreate your action and then do own action like create sms or call to someone.

Here is my code:

 //gcm part, found on google start guid with gcm, modified some
 //my method takes some data stored in contentValues, this just an example  
 private void sendNotification(ContentValues cv) {  
      mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);  
   
      Intent intent = new Intent(this, StatPendingActivity.class);//CUSTOM ACTIVITY HERE  
   
      Bundle parcerable = new Bundle();  
      parcerable.putParcelable("data", cv);  
   
      intent.putExtra("parcelable", parcerable);  
   
      //intent drops into pendingIntent, REMEMBER USE FLAG_UPDATE_CURRENT to be able pass data!  
      PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);  
   
      NotificationCompat.Builder mBuilder =  
                new NotificationCompat.Builder(this)  
                          .setSmallIcon(android.R.drawable.presence_video_busy)  
                          .setLargeIcon(((BitmapDrawable) getResources().getDrawable(android.R.drawable.presence_video_busy)).getBitmap())  
                          .setContentTitle(cv.getAsString(key_title))  
                          .setStyle(new NotificationCompat.BigTextStyle()  
                                    .bigText(cv.getAsString(key_message)))  
                          .setContentText(cv.getAsString(key_message));  
   
      mBuilder.setContentIntent(contentIntent);  
   
      //FLAG TO SHOW ONLY ONE NOTIFICATION  
      Notification build = mBuilder.build();  
      build.flags |= Notification.FLAG_AUTO_CANCEL;  
      mNotificationManager.notify(NOTIFICATION_ID, build);  
 }  

And here is my custom Activity :

 public class StatPendingActivity extends Activity {  
   
   private String url = "http://localhost";  
   
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
   
           //this just an example, I takes some data from GCM which was in previous method  
     Bundle parcelable = getIntent().getBundleExtra("parcelable");  
     if (parcelable != null) {  
   
       ContentValues cv = parcelable.getParcelable("data");  
       Intent intent = new Intent();  
   
                //here I do some stuff with data from GCM  
       if (cv.getAsString(key_action).equals("CLICK_TO_SMS")) {  
         String smsNumber = cv.getAsString(key_sms_number);  
         String smsMessage = cv.getAsString(key_sms_message);  
   
         Uri uriNumber = Uri.parse("smsto:" + smsNumber);  
         intent = new Intent(Intent.ACTION_SENDTO, uriNumber);  
         intent.putExtra("sms_body", smsMessage);  
   
       }  
                //calling like notification some action, here is sending the sms  
                //we do not want remember this activity because it should behave like normal notification  
       intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);  
       startActivity(intent);  
   
                //my custom action, something like onClickListener  
                //this method call asynctask, so it wont be killed if activity is destroyed  
       sendClickStat(cv);//my action to do when I click notification  
   
       finish();//destroying this activity  
     }  
   }  
   
 }