Advertisement
Guest User

PreFroyoNotificationStyleDiscover

a guest
Feb 10th, 2012
869
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.23 KB | None | 0 0
  1. package com.contentwatch.ghoti.cp;
  2.  
  3. import android.app.Notification;
  4. import android.content.Context;
  5. import android.util.DisplayMetrics;
  6. import android.view.ViewGroup;
  7. import android.view.WindowManager;
  8. import android.widget.LinearLayout;
  9. import android.widget.TextView;
  10.  
  11. /// A class for discovering Android Notification styles on Pre-Froyo (2.3) devices
  12. /// @remarks    See http://stackoverflow.com/questions/4867338/custom-notification-layouts-and-text-colors/7320604#7320604
  13. public class PreFroyoNotificationStyleDiscover {
  14.  
  15.     private Integer mNotifyTextColor                = null;
  16.     private float   mNotifyTextSize                 = 11;
  17.     private Integer mNotifyTitleColor               = null;
  18.     private float   mNotifyTitleSize                = 12;
  19.     private final String TEXT_SEARCH_TEXT           = "SearchForText";
  20.     private final String TEXT_SEARCH_TITLE          = "SearchForTitle";
  21.     private Context mContext;
  22.    
  23.     PreFroyoNotificationStyleDiscover(Context context) {
  24.         mContext = context;
  25.         discoverStyle();
  26.     }
  27.    
  28.     public int getTextColor() {
  29.         return mNotifyTextColor.intValue();
  30.     }
  31.    
  32.     public float getTextSize() {
  33.         return mNotifyTextSize;
  34.     }
  35.    
  36.     public int getTitleColor() {
  37.         return mNotifyTitleColor;
  38.     }
  39.    
  40.     public float getTitleSize() {
  41.         return mNotifyTitleSize;
  42.     }
  43.    
  44.     private boolean recurseGroup(ViewGroup group) {
  45.         final int count = group.getChildCount();
  46.        
  47.         for (int i = 0; i < count; ++i) {
  48.             if(group.getChildAt(i) instanceof TextView) {
  49.                 final TextView tv = (TextView)group.getChildAt(i);
  50.                 final String text = tv.getText().toString();
  51.                 if(text.startsWith("SearchFor")) {
  52.                     DisplayMetrics metrics = new DisplayMetrics();
  53.                     WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
  54.                     wm.getDefaultDisplay().getMetrics(metrics);
  55.                    
  56.                     if(TEXT_SEARCH_TEXT == text) {
  57.                         mNotifyTextColor    = tv.getTextColors().getDefaultColor();
  58.                         mNotifyTextSize     = tv.getTextSize();
  59.                         mNotifyTextSize     /= metrics.scaledDensity;
  60.                     } else {
  61.                         mNotifyTitleColor   = tv.getTextColors().getDefaultColor();
  62.                         mNotifyTitleSize    = tv.getTextSize();
  63.                         mNotifyTitleSize    /= metrics.scaledDensity;
  64.                     }
  65.                    
  66.                     if(null != mNotifyTitleColor && mNotifyTextColor != null) {
  67.                         return true;
  68.                     }
  69.                 }
  70.             } else if (group.getChildAt(i) instanceof ViewGroup) {
  71.                 if(recurseGroup((ViewGroup)group.getChildAt(i))) {
  72.                     return true;
  73.                 }
  74.             }
  75.         }
  76.         return false;
  77.     }
  78.  
  79.     private void discoverStyle() {
  80.         if(null != mNotifyTextColor) {
  81.             //  already done
  82.             return;
  83.         }
  84.        
  85.         try {
  86.             Notification notify = new Notification();
  87.             notify.setLatestEventInfo(mContext, TEXT_SEARCH_TITLE, TEXT_SEARCH_TEXT, null);
  88.             LinearLayout group = new LinearLayout(mContext);
  89.             ViewGroup event = (ViewGroup)notify.contentView.apply(mContext, group);
  90.             recurseGroup(event);
  91.             group.removeAllViews();
  92.         } catch (Exception e) {
  93.             //  default to something
  94.             mNotifyTextColor    = android.R.color.black;
  95.             mNotifyTitleColor   = android.R.color.black;
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement