Android Open Source - CompleteActionPlus Rule Item Helper






From Project

Back to project page CompleteActionPlus.

License

The source code is released under:

## Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International Public License ## By exercising the Licensed Rights (defined below), You accept and agree to be bound by the terms and condi...

If you think the Android project CompleteActionPlus listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package hk.valenta.completeactionplus;
/* ww w .j a  v a 2 s  .co m*/
import android.content.ComponentName;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.drawable.Drawable;
import android.support.v4.app.FragmentActivity;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

public class RuleItemHelper {
  public final static void createRuleAppElement(FragmentActivity activity, PackageManager pm, LinearLayout list, String pkg) {
    try {
      // get information
      String name = null;
      Drawable icon = null;
      
      // exist in cache?
      ManagerPagerActivity mActivity = (ManagerPagerActivity)activity;
      if (mActivity.cachePackage.contains(pkg)) {
        // get it from cache
        int index = mActivity.cachePackage.indexOf(pkg);
        name = mActivity.cacheNames.get(index);
        icon = mActivity.cacheIcons.get(index);
      } else if (pkg.contains("/")) {
        // get activity info
        ActivityInfo aInfo = pm.getActivityInfo(ComponentName.unflattenFromString(pkg), PackageManager.GET_ACTIVITIES);
        name = aInfo.loadLabel(pm).toString();
        icon = aInfo.loadIcon(pm);
        
        // add to cache
        mActivity.cachePackage.add(pkg);
        mActivity.cacheNames.add(name);
        mActivity.cacheIcons.add(icon);
      } else {
        // get info
        PackageInfo info = pm.getPackageInfo(pkg, PackageManager.GET_ACTIVITIES);
        name = info.applicationInfo.loadLabel(pm).toString();
        icon = info.applicationInfo.loadIcon(pm);
        
        // add to cache
        mActivity.cachePackage.add(pkg);
        mActivity.cacheNames.add(name);
        mActivity.cacheIcons.add(icon);
      }
      
      // setup layout
      LinearLayout layout = new LinearLayout(activity);
      layout.setOrientation(LinearLayout.VERTICAL);
      list.addView(layout);
      DisplayMetrics metrics = activity.getResources().getDisplayMetrics();
      LinearLayout.LayoutParams params = (LayoutParams)layout.getLayoutParams();
      params.width = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 60, metrics);
      params.height = LayoutParams.WRAP_CONTENT;    
      params.gravity = Gravity.TOP;
      
      // setup icon
      ImageView img = new ImageView(activity);
      img.setScaleType(ScaleType.FIT_XY);
      img.setImageDrawable(icon);
//      img.setImageResource(R.drawable.ic_launcher);
      layout.addView(img);
      params = (LayoutParams)img.getLayoutParams();
      params.width = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 48, metrics);
      params.height = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 48, metrics);
      params.gravity = Gravity.CENTER_HORIZONTAL;
      
      // setup text
      TextView text = new TextView(activity);
      text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
      text.setLines(2);
      text.setGravity(Gravity.CENTER);
      text.setText(name);
      layout.addView(text);
      params = (LayoutParams)text.getLayoutParams();
      params.width = LayoutParams.WRAP_CONTENT;
      params.height = LayoutParams.WRAP_CONTENT;
      params.gravity = Gravity.CENTER_HORIZONTAL;
    } catch (NameNotFoundException e) {
      // nothing to do
      return;
    } catch (Exception e) {
      // not exist anymore
      e.printStackTrace();
    }
  }
}




Java Source Code List

hk.valenta.completeactionplus.AboutFragment.java
hk.valenta.completeactionplus.AddedFragment.java
hk.valenta.completeactionplus.AdvancedFragment.java
hk.valenta.completeactionplus.ColorPicker.java
hk.valenta.completeactionplus.DialogFragment.java
hk.valenta.completeactionplus.EnumConvert.java
hk.valenta.completeactionplus.FavoriteFragment.java
hk.valenta.completeactionplus.HiddenFragment.java
hk.valenta.completeactionplus.IntentRecorderActivity.java
hk.valenta.completeactionplus.IntentRecorderReceiver.java
hk.valenta.completeactionplus.LayoutFragment.java
hk.valenta.completeactionplus.MainPagerActivity.java
hk.valenta.completeactionplus.ManageListActivity.java
hk.valenta.completeactionplus.ManagerPagerActivity.java
hk.valenta.completeactionplus.RuleItemHelper.java
hk.valenta.completeactionplus.SelectListActivity.java
hk.valenta.completeactionplus.StartSelectedReceiver.java
hk.valenta.completeactionplus.TemporaryDefaultReceiver.java
hk.valenta.completeactionplus.TestIntentActivity.java
hk.valenta.completeactionplus.XCompleteActionPlus.java