Example usage for android.content.res Resources getIdentifier

List of usage examples for android.content.res Resources getIdentifier

Introduction

In this page you can find the example usage for android.content.res Resources getIdentifier.

Prototype

public int getIdentifier(String name, String defType, String defPackage) 

Source Link

Document

Return a resource identifier for the given resource name.

Usage

From source file:org.openintents.notepad.NoteEditor.java

private boolean setRemoteStyle(String styleName, int size) {
    if (TextUtils.isEmpty(styleName)) {
        if (DEBUG) {
            Log.e(TAG, "Empty style name: " + styleName);
        }/*from w  w  w . jav a  2 s  .  c o  m*/
        return false;
    }

    PackageManager pm = getPackageManager();

    String packageName = ThemeUtils.getPackageNameFromStyle(styleName);

    if (packageName == null) {
        Log.e(TAG, "Invalid style name: " + styleName);
        return false;
    }

    Context c = null;
    try {
        c = createPackageContext(packageName, 0);
    } catch (NameNotFoundException e) {
        Log.e(TAG, "Package for style not found: " + packageName + ", " + styleName);
        return false;
    }

    Resources res = c.getResources();

    int themeid = res.getIdentifier(styleName, null, null);
    if (DEBUG) {
        Log.d(TAG, "Retrieving theme: " + styleName + ", " + themeid);
    }

    if (themeid == 0) {
        Log.e(TAG, "Theme name not found: " + styleName);
        return false;
    }

    try {
        ThemeAttributes ta = new ThemeAttributes(c, packageName, themeid);

        mTextTypeface = ta.getString(ThemeNotepad.TEXT_TYPEFACE);
        if (DEBUG) {
            Log.d(TAG, "textTypeface: " + mTextTypeface);
        }

        mCurrentTypeface = null;

        // Look for special cases:
        if ("monospace".equals(mTextTypeface)) {
            mCurrentTypeface = Typeface.create(Typeface.MONOSPACE, Typeface.NORMAL);
        } else if ("sans".equals(mTextTypeface)) {
            mCurrentTypeface = Typeface.create(Typeface.SANS_SERIF, Typeface.NORMAL);
        } else if ("serif".equals(mTextTypeface)) {
            mCurrentTypeface = Typeface.create(Typeface.SERIF, Typeface.NORMAL);
        } else if (!TextUtils.isEmpty(mTextTypeface)) {

            try {
                if (DEBUG) {
                    Log.d(TAG, "Reading typeface: package: " + packageName + ", typeface: " + mTextTypeface);
                }
                Resources remoteRes = pm.getResourcesForApplication(packageName);
                mCurrentTypeface = Typeface.createFromAsset(remoteRes.getAssets(), mTextTypeface);
                if (DEBUG) {
                    Log.d(TAG, "Result: " + mCurrentTypeface);
                }
            } catch (NameNotFoundException e) {
                Log.e(TAG, "Package not found for Typeface", e);
            }
        }

        mTextUpperCaseFont = ta.getBoolean(ThemeNotepad.TEXT_UPPER_CASE_FONT, false);

        mTextColor = ta.getColor(ThemeNotepad.TEXT_COLOR, android.R.color.white);

        if (DEBUG) {
            Log.d(TAG, "textColor: " + mTextColor);
        }

        if (size == 0) {
            mTextSize = getTextSizeTiny(ta);
        } else if (size == 1) {
            mTextSize = getTextSizeSmall(ta);
        } else if (size == 2) {
            mTextSize = getTextSizeMedium(ta);
        } else {
            mTextSize = getTextSizeLarge(ta);
        }
        if (DEBUG) {
            Log.d(TAG, "textSize: " + mTextSize);
        }

        if (mText != null) {
            mBackgroundPadding = ta.getDimensionPixelOffset(ThemeNotepad.BACKGROUND_PADDING, -1);
            int backgroundPaddingLeft = ta.getDimensionPixelOffset(ThemeNotepad.BACKGROUND_PADDING_LEFT,
                    mBackgroundPadding);
            int backgroundPaddingTop = ta.getDimensionPixelOffset(ThemeNotepad.BACKGROUND_PADDING_TOP,
                    mBackgroundPadding);
            int backgroundPaddingRight = ta.getDimensionPixelOffset(ThemeNotepad.BACKGROUND_PADDING_RIGHT,
                    mBackgroundPadding);
            int backgroundPaddingBottom = ta.getDimensionPixelOffset(ThemeNotepad.BACKGROUND_PADDING_BOTTOM,
                    mBackgroundPadding);

            if (DEBUG) {
                Log.d(TAG,
                        "Padding: " + mBackgroundPadding + "; " + backgroundPaddingLeft + "; "
                                + backgroundPaddingTop + "; " + backgroundPaddingRight + "; "
                                + backgroundPaddingBottom + "; ");
            }

            try {
                Resources remoteRes = pm.getResourcesForApplication(packageName);
                int resid = ta.getResourceId(ThemeNotepad.BACKGROUND, 0);
                if (resid != 0) {
                    Drawable d = remoteRes.getDrawable(resid);
                    mText.setBackgroundDrawable(d);
                } else {
                    // remove background
                    mText.setBackgroundResource(0);
                }
            } catch (NameNotFoundException e) {
                Log.e(TAG, "Package not found for Theme background.", e);
            } catch (Resources.NotFoundException e) {
                Log.e(TAG, "Resource not found for Theme background.", e);
            }

            // Apply padding
            if (mBackgroundPadding >= 0 || backgroundPaddingLeft >= 0 || backgroundPaddingTop >= 0
                    || backgroundPaddingRight >= 0 || backgroundPaddingBottom >= 0) {
                mText.setPadding(backgroundPaddingLeft, backgroundPaddingTop, backgroundPaddingRight,
                        backgroundPaddingBottom);
            } else {
                // 9-patches do the padding automatically
                // todo clear padding
            }
        }

        mLinesMode = ta.getInteger(ThemeNotepad.LINE_MODE, 2);
        mLinesColor = ta.getColor(ThemeNotepad.LINE_COLOR, 0xFF000080);

        if (DEBUG) {
            Log.d(TAG, "line color: " + mLinesColor);
        }

        return true;

    } catch (UnsupportedOperationException e) {
        // This exception is thrown e.g. if one attempts
        // to read an integer attribute as dimension.
        Log.e(TAG, "UnsupportedOperationException", e);
        return false;
    } catch (NumberFormatException e) {
        // This exception is thrown e.g. if one attempts
        // to read a string as integer.
        Log.e(TAG, "NumberFormatException", e);
        return false;
    }
}

From source file:com.androzic.Androzic.java

public void initializePlugins() {
    PackageManager packageManager = getPackageManager();
    List<ResolveInfo> plugins;
    Intent initializationIntent = new Intent("com.androzic.plugins.action.INITIALIZE");

    // enumerate initializable plugins
    plugins = packageManager.queryBroadcastReceivers(initializationIntent, 0);
    for (ResolveInfo plugin : plugins) {
        // send initialization broadcast, we send it directly instead of sending
        // one broadcast for all plugins to wake up stopped plugins:
        // http://developer.android.com/about/versions/android-3.1.html#launchcontrols
        Intent intent = new Intent();
        intent.setClassName(plugin.activityInfo.packageName, plugin.activityInfo.name);
        intent.setAction(initializationIntent.getAction());
        sendBroadcast(intent);// w  w  w . j  a  v a2 s  .  com
    }

    // enumerate plugins with preferences
    plugins = packageManager.queryIntentActivities(new Intent("com.androzic.plugins.preferences"), 0);
    for (ResolveInfo plugin : plugins) {
        Intent intent = new Intent();
        intent.setClassName(plugin.activityInfo.packageName, plugin.activityInfo.name);
        pluginPreferences.put(plugin.activityInfo.loadLabel(packageManager).toString(), intent);
    }

    // enumerate plugins with views
    plugins = packageManager.queryIntentActivities(new Intent("com.androzic.plugins.view"), 0);
    for (ResolveInfo plugin : plugins) {
        // get menu icon
        Drawable icon = null;
        try {
            Resources resources = packageManager
                    .getResourcesForApplication(plugin.activityInfo.applicationInfo);
            int id = resources.getIdentifier("ic_menu_view", "drawable", plugin.activityInfo.packageName);
            if (id != 0)
                icon = resources.getDrawable(id);
        } catch (Resources.NotFoundException e) {
            e.printStackTrace();
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        Intent intent = new Intent();
        intent.setClassName(plugin.activityInfo.packageName, plugin.activityInfo.name);
        Pair<Drawable, Intent> pair = new Pair<>(icon, intent);
        pluginViews.put(plugin.activityInfo.loadLabel(packageManager).toString(), pair);
    }
}

From source file:com.bookkos.bircle.CaptureActivity.java

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    _context = getApplicationContext();/* ww w  .ja  va  2  s.com*/
    _activity = this;

    currentTime = new Time("Asia/Tokyo");

    //      exceptionHandler = new ExceptionHandler(_context);   
    //      Thread.setDefaultUncaughtExceptionHandler(exceptionHandler);

    // sharedPreference???, user_id?group_id?registration_id??
    getUserData();

    Window window = getWindow();
    window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    // ??
    WindowManager window_manager = getWindowManager();
    Display display = window_manager.getDefaultDisplay();
    Point point = new Point();
    display.getSize(point);
    displayWidth = point.x;
    displayHeight = point.y;

    displayInch = getInch();
    // ??4???????
    textSize = 17 * (displayInch / 4);

    actionBar = getActionBar();
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE, ActionBar.DISPLAY_USE_LOGO);
    actionBar.setDisplayShowTitleEnabled(true);
    actionBar.setDisplayUseLogoEnabled(false);
    actionBar.setDisplayShowHomeEnabled(false);
    actionBar.setDisplayHomeAsUpEnabled(false);
    String title_text = "";
    subGroupText = "";
    groupText = groupName;

    if (displayInch < 4.7) {
        title_text = "<small><small><small>??: </small></small></small>";
        resizeTitleSizeTooSmall();
    } else if (displayInch >= 4.7 && displayInch < 5.5) {
        title_text = "<small><small>??: </small></small>";
        resizeTitleSizeSmall();
    } else if (displayInch >= 5.5 && displayInch < 6.5) {
        title_text = "<small>??: </small>";
        resizeTitleSizeMiddle();
    } else if (displayInch >= 6.5 && displayInch < 8) {
        title_text = "<small>??: </small>";
        resizeTitleSizeLarge();
    } else {
        title_text = "??: ";
    }
    String modify_group_text = title_text + "<font color=#FF0000>" + groupName + "</font>";
    actionBar.setTitle(Html.fromHtml(modify_group_text));

    Resources resources = _context.getResources();
    int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
    titleBarHeight = resources.getDimensionPixelSize(resourceId);

    setContentView(R.layout.capture);

    returnBorrowHelpView = (ImageView) findViewById(R.id.return_borrow_help_view);
    returnBorrowHelpView.setImageResource(R.drawable.return_borrow_help);
    returnBorrowHelpView.setTranslationY(displayHeight / 5 + titleBarHeight);
    returnBorrowHelpView.setLayoutParams(new FrameLayout.LayoutParams(displayWidth,
            displayHeight / 5 + titleBarHeight, Gravity.BOTTOM | Gravity.CENTER));

    registHelpView = (ImageView) findViewById(R.id.regist_help_view);
    registHelpView.setImageResource(R.drawable.regist_help);
    registHelpView.setTranslationY(displayHeight / 5 + titleBarHeight);
    registHelpView.setLayoutParams(new FrameLayout.LayoutParams(displayWidth,
            displayHeight / 5 + titleBarHeight, Gravity.BOTTOM | Gravity.CENTER));
    registHelpView.setVisibility(View.GONE);

    leftDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    leftDrawer = (ListView) findViewById(R.id.left_drawer);
    textView = (TextView) findViewById(R.id.textView);

    modeText = (TextView) findViewById(R.id.mode_text);
    modeText.setTextColor(Color.rgb(56, 234, 123));
    modeText.setTextSize(textSize);
    modeText.setTypeface(Typeface.SERIF.MONOSPACE, Typeface.BOLD);
    strokeColor = Color.rgb(56, 234, 123);

    borrowReturnButton = (Button) findViewById(R.id.borrowReturnButton);
    registButton = (Button) findViewById(R.id.registButton);
    returnHistoryButton = (Button) findViewById(R.id.return_history_button);
    helpViewButton = (Button) findViewById(R.id.help_view_button);

    registSelectShelfRelativeLayout = (RelativeLayout) findViewById(R.id.regist_select_shelf_relative_layout);
    textViewLinearLayout = (LinearLayout) findViewById(R.id.text_view_linear_layout);
    buttonLinearLayout = (LinearLayout) findViewById(R.id.button_linear_layout);
    listViewLinearLayout = (LinearLayout) findViewById(R.id.list_view_linear_layout);
    decisionButton = (Button) findViewById(R.id.decision_button);
    cancelButton = (Button) findViewById(R.id.cancel_button);
    shelfListView = (ListView) findViewById(R.id.shelf_list_view);
    tempTextView = (TextView) findViewById(R.id.temp_text_view);
    //      bookListViewAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
    bookListViewAdapter = new BookListViewAdapter(_context, R.layout.book_list_row, this);

    bookRegistRelativeLayout = (RelativeLayout) findViewById(R.id.book_regist_relative_layout);
    bookRegistLinearLayout = (LinearLayout) findViewById(R.id.book_regist_linear_layout);
    bookRegistListViewLinearLayout = (LinearLayout) findViewById(R.id.book_regist_list_view_linear_layout);
    bookRegistListView = (ListView) findViewById(R.id.book_regist_list_view);
    bookRegistTextView = (TextView) findViewById(R.id.book_regist_text_view);
    bookRegistCancelButton = (Button) findViewById(R.id.book_regist_cancel_button);

    registFlag = 0;

    int borrowReturnButton_width = displayWidth / 5 * 2;
    int borrowReturnButton_height = displayHeight / 10;
    int borrowReturnButton_x = ((displayWidth / 2) - borrowReturnButton_width) / 2;
    int borrowReturnButton_y = displayHeight / 2 + titleBarHeight;
    borrowReturnButton.setTranslationX(borrowReturnButton_x);
    borrowReturnButton.setTranslationY(borrowReturnButton_y);
    borrowReturnButton
            .setLayoutParams(new FrameLayout.LayoutParams(borrowReturnButton_width, borrowReturnButton_height));
    borrowReturnButton.setBackgroundColor(Color.rgb(56, 234, 123));
    borrowReturnButton.setText("??\n");
    borrowReturnButton.setTextSize(textSize * 7 / 10);
    borrowReturnButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            arrayList.clear();
            registFlag = 0;

            borrowReturnButton.setText("??\n");
            borrowReturnButton.setEnabled(false);
            borrowReturnButton.setTextColor(Color.WHITE);
            borrowReturnButton.setBackgroundColor(Color.rgb(56, 234, 123));

            registButton.setText("?\n??");
            registButton.setEnabled(true);
            registButton.setTextColor(Color.GRAY);
            registButton.setBackgroundColor(Color.argb(170, 21, 38, 45));

            modeText.setText("??");
            modeText.setTextColor(Color.rgb(56, 234, 123));
            returnBorrowHelpView.setVisibility(View.VISIBLE);
            registHelpView.setVisibility(View.GONE);
            strokeColor = Color.rgb(56, 234, 123);
        }
    });

    int registButton_width = displayWidth / 5 * 2;
    int registButton_height = displayHeight / 10;
    int registButton_x = (displayWidth / 2) + ((displayWidth / 2) - registButton_width) / 2;
    int registButton_y = displayHeight / 2 + titleBarHeight;
    registButton.setTranslationX(registButton_x);
    registButton.setTranslationY(registButton_y);
    registButton.setLayoutParams(new FrameLayout.LayoutParams(registButton_width, registButton_height));
    registButton.setBackgroundColor(Color.argb(170, 21, 38, 45));
    registButton.setTextColor(Color.GRAY);
    registButton.setTextSize(textSize * 7 / 10);
    registButton.setText("?\n??");
    registButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            arrayList.clear();
            registFlag = 1;

            borrowReturnButton.setText("??\n??");
            borrowReturnButton.setEnabled(true);
            borrowReturnButton.setTextColor(Color.GRAY);
            borrowReturnButton.setBackgroundColor(Color.argb(170, 9, 54, 16));

            registButton.setText("?\n");
            registButton.setEnabled(false);
            registButton.setTextColor(Color.WHITE);
            registButton.setBackgroundColor(Color.rgb(62, 162, 229));

            modeText.setText("?");
            modeText.setTextColor(Color.rgb(62, 162, 229));
            returnBorrowHelpView.setVisibility(View.GONE);
            registHelpView.setVisibility(View.VISIBLE);
            strokeColor = Color.rgb(62, 162, 229);
        }
    });

    returnHistoryButton.setText("????");
    returnHistoryButton.setTextSize(textSize * 7 / 10);
    returnHistoryButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            leftDrawerLayout.openDrawer(Gravity.RIGHT);
            //            animateTranslationY(bookRegistRelativeLayout, displayHeight, displayHeight - displayHeight / 4 - titleBarHeight);
        }
    });
    getReturnHistory();
    getCurrentTime();

    setHelpView();
    setScanUnregisterBookView();
    setBookRegistView();

    arrayList = new ArrayList<String>();
    tempRegistIsbn = "";

    initBookRegistUrl = book_register_url + "?user_id=" + userId + "&group_id=" + groupId;
    initLendRegistUrl = lend_register_url + "?user_id=" + userId + "&group_id=" + groupId;
    initTemporaryLendRegistUrl = temporary_lend_register_url + "?user_id=" + userId + "&group_id=" + groupId;
    initCatalogRegistUrl = catalog_register_url + "?group_id=" + groupId + "&book_code=";
    initManuallyCatalogRegistUrl = manually_catalog_register_url + "?group_id=" + groupId + "&book_code=";
    getStatusUrl = get_status_url + "?group_id=" + groupId + "&user_id=" + userId;

    hasSurface = false;

    inactivityTimer = new InactivityTimer(this);
    bircleBeepManager = new BircleBeepManager(this);
    ambientLightManager = new AmbientLightManager(this);

    PreferenceManager.setDefaultValues(this, R.xml.preferences, false);

    toastText = "";
}

From source file:org.distantshoresmedia.keyboard.LatinIME.java

/**
 * Loads a dictionary or multiple separated dictionary
 *
 * @return returns array of dictionary resource ids
 *///from  w  w  w .j a v a 2s  . c  om
/* package */static int[] getDictionary(Resources res) {
    String packageName = LatinIME.class.getPackage().getName();
    XmlResourceParser xrp = res.getXml(R.xml.dictionary);
    ArrayList<Integer> dictionaries = new ArrayList<Integer>();

    try {
        int current = xrp.getEventType();
        while (current != XmlResourceParser.END_DOCUMENT) {
            if (current == XmlResourceParser.START_TAG) {
                String tag = xrp.getName();
                if (tag != null) {
                    if (tag.equals("part")) {
                        String dictFileName = xrp.getAttributeValue(null, "name");
                        dictionaries.add(res.getIdentifier(dictFileName, "raw", packageName));
                    }
                }
            }
            xrp.next();
            current = xrp.getEventType();
        }
    } catch (XmlPullParserException e) {
        Log.e(TAG, "Dictionary XML parsing failure");
    } catch (IOException e) {
        Log.e(TAG, "Dictionary XML IOException");
    }

    int count = dictionaries.size();
    int[] dict = new int[count];
    for (int i = 0; i < count; i++) {
        dict[i] = dictionaries.get(i);
    }

    return dict;
}

From source file:de.vanita5.twittnuker.util.Utils.java

public static int getResId(final Context context, final String string) {
    if (context == null || string == null)
        return 0;
    Matcher m = PATTERN_RESOURCE_IDENTIFIER.matcher(string);
    final Resources res = context.getResources();
    if (m.matches())
        return res.getIdentifier(m.group(2), m.group(1), context.getPackageName());
    m = PATTERN_XML_RESOURCE_IDENTIFIER.matcher(string);
    if (m.matches())
        return res.getIdentifier(m.group(1), "xml", context.getPackageName());
    return 0;//w  w w.  j av  a  2 s  .  c o  m
}

From source file:com.cognizant.trumobi.PersonaLauncher.java

static PersonaLiveFolderInfo addLiveFolder(Context context, Intent data, PersonaCellLayout.CellInfo cellInfo,
        boolean notify) {

    Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
    String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);

    Drawable icon = null;//from w ww .  j  ava 2  s .c  om
    boolean filtered = false;
    Intent.ShortcutIconResource iconResource = null;

    Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
    if (extra != null && extra instanceof Intent.ShortcutIconResource) {
        try {
            iconResource = (Intent.ShortcutIconResource) extra;
            final PackageManager packageManager = context.getPackageManager();
            Resources resources = packageManager.getResourcesForApplication(iconResource.packageName);
            final int id = resources.getIdentifier(iconResource.resourceName, null, null);
            icon = resources.getDrawable(id);
        } catch (Exception e) {
            PersonaLog.w(LOG_TAG, "Could not load live folder icon: " + extra);
        }
    }

    if (icon == null) {
        icon = context.getResources().getDrawable(R.drawable.pr_ic_launcher_folder);
    }

    final PersonaLiveFolderInfo info = new PersonaLiveFolderInfo();
    info.icon = icon;
    info.filtered = filtered;
    info.title = name;
    info.iconResource = iconResource;
    info.uri = data.getData();
    info.baseIntent = baseIntent;
    info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
            LiveFolders.DISPLAY_MODE_GRID);

    PersonaLauncherModel.addItemToDatabase(context, info, PersonaLauncherSettings.Favorites.CONTAINER_DESKTOP,
            cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
    sModel.addFolder(info);

    return info;
}

From source file:com.cognizant.trumobi.PersonaLauncher.java

private static PersonaApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
    Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
    String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
    Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);

    Drawable icon = null;//ww w .j  av  a  2  s . co  m
    boolean filtered = false;
    boolean customIcon = false;
    ShortcutIconResource iconResource = null;

    if (bitmap != null) {
        icon = new PersonaFastBitmapDrawable(PersonaUtilities.createBitmapThumbnail(bitmap, context));
        filtered = true;
        customIcon = true;
    } else {
        Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
        if (extra != null && extra instanceof ShortcutIconResource) {
            try {
                iconResource = (ShortcutIconResource) extra;
                final PackageManager packageManager = context.getPackageManager();
                Resources resources = packageManager.getResourcesForApplication(iconResource.packageName);
                final int id = resources.getIdentifier(iconResource.resourceName, null, null);
                icon = resources.getDrawable(id);
            } catch (Exception e) {
                PersonaLog.w(LOG_TAG, "Could not load shortcut icon: " + extra);
            }
        }
    }

    if (icon == null) {
        icon = context.getPackageManager().getDefaultActivityIcon();
    }

    final PersonaApplicationInfo info = new PersonaApplicationInfo();
    info.icon = icon;
    info.filtered = filtered;
    info.title = name;
    info.intent = intent;
    info.customIcon = customIcon;
    info.iconResource = iconResource;

    return info;
}

From source file:com.cognizant.trumobi.PersonaLauncher.java

/**
 * ADW: Load the specified theme resource
 * //from  ww w  . j  a va 2s. c  o m
 * @param themeResources
 *            Resources from the theme package
 * @param themePackage
 *            the theme's package name
 * @param item_name
 *            the theme item name to load
 * @param item
 *            the View Item to apply the theme into
 * @param themeType
 *            Specify if the themed element will be a background or a
 *            foreground item
 */
public static void loadThemeResource(Resources themeResources, String themePackage, String item_name, View item,
        int themeType) {
    Drawable d = null;
    if (themeResources != null) {
        int resource_id = themeResources.getIdentifier(item_name, "drawable", themePackage);
        if (resource_id != 0) {
            try {
                d = themeResources.getDrawable(resource_id);
            } catch (Resources.NotFoundException e) {
                return;
            }
            if (themeType == THEME_ITEM_FOREGROUND && item instanceof ImageView) {
                // ADW remove the old drawable
                Drawable tmp = ((ImageView) item).getDrawable();
                if (tmp != null) {
                    tmp.setCallback(null);
                    tmp = null;
                }
                ((ImageView) item).setImageDrawable(d);
            } else {
                // ADW remove the old drawable
                Drawable tmp = item.getBackground();
                if (tmp != null) {
                    tmp.setCallback(null);
                    tmp = null;
                }
                item.setBackgroundDrawable(d);
            }
        }
    }
}

From source file:com.cognizant.trumobi.PersonaLauncher.java

private void completeEditShirtcut(Intent data) {
    if (!data.hasExtra(PersonaCustomShirtcutActivity.EXTRA_APPLICATIONINFO))
        return;/*from w w w  .  java2s  .  c o m*/
    long appInfoId = data.getLongExtra(PersonaCustomShirtcutActivity.EXTRA_APPLICATIONINFO, 0);
    PersonaApplicationInfo info = PersonaLauncherModel.loadApplicationInfoById(this, appInfoId);
    if (info != null) {
        Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);

        Drawable icon = null;
        boolean customIcon = false;
        ShortcutIconResource iconResource = null;

        if (bitmap != null) {
            icon = new PersonaFastBitmapDrawable(PersonaUtilities.createBitmapThumbnail(bitmap, this));
            customIcon = true;
        } else {
            Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
            if (extra != null && extra instanceof ShortcutIconResource) {
                try {
                    iconResource = (ShortcutIconResource) extra;
                    final PackageManager packageManager = getPackageManager();
                    Resources resources = packageManager.getResourcesForApplication(iconResource.packageName);
                    final int id = resources.getIdentifier(iconResource.resourceName, null, null);
                    icon = resources.getDrawable(id);
                } catch (Exception e) {
                    PersonaLog.w(LOG_TAG, "Could not load shortcut icon: " + extra);
                }
            }
        }

        if (icon != null) {
            info.icon = icon;
            info.customIcon = customIcon;
            info.iconResource = iconResource;
        }
        info.itemType = PersonaLauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
        info.title = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
        info.intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
        PersonaLauncherModel.updateItemInDatabase(this, info);

        if (info.container == PersonaLauncherSettings.Favorites.CONTAINER_MAB)
            mHandleView.UpdateLaunchInfo(info);
        else if (info.container == PersonaLauncherSettings.Favorites.CONTAINER_LAB)
            mLAB.UpdateLaunchInfo(info);
        else if (info.container == PersonaLauncherSettings.Favorites.CONTAINER_LAB2)
            mLAB2.UpdateLaunchInfo(info);
        else if (info.container == PersonaLauncherSettings.Favorites.CONTAINER_RAB)
            mRAB.UpdateLaunchInfo(info);
        else if (info.container == PersonaLauncherSettings.Favorites.CONTAINER_RAB2)
            mRAB2.UpdateLaunchInfo(info);

        mWorkspace.updateShortcutFromApplicationInfo(info);
    }
}

From source file:g7.bluesky.launcher3.Launcher.java

public void loadIconPack(List<ShortcutInfo> shortcuts) {
    //theming vars-----------------------------------------------
    PackageManager pm = getPackageManager();
    final int ICONSIZE = Tools.numtodp(128, Launcher.this);
    Resources themeRes = null;
    String resPacName = defaultSharedPref.getString(SettingConstants.ICON_THEME_PREF_KEY, "");
    String iconResource = null;//from w w  w . jav  a  2  s .c om
    int intres = 0;
    int intresiconback = 0;
    int intresiconfront = 0;
    int intresiconmask = 0;
    float scaleFactor = 1.0f;

    Paint p = new Paint(Paint.FILTER_BITMAP_FLAG);
    p.setAntiAlias(true);

    Paint origP = new Paint(Paint.FILTER_BITMAP_FLAG);
    origP.setAntiAlias(true);

    Paint maskp = new Paint(Paint.FILTER_BITMAP_FLAG);
    maskp.setAntiAlias(true);
    maskp.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));

    if (resPacName.compareTo("") != 0) {
        try {
            themeRes = pm.getResourcesForApplication(resPacName);
        } catch (Exception e) {
        }
        ;
        if (themeRes != null) {
            String[] backAndMaskAndFront = ThemeTools.getIconBackAndMaskResourceName(themeRes, resPacName);
            if (backAndMaskAndFront[0] != null)
                intresiconback = themeRes.getIdentifier(backAndMaskAndFront[0], "drawable", resPacName);
            if (backAndMaskAndFront[1] != null)
                intresiconmask = themeRes.getIdentifier(backAndMaskAndFront[1], "drawable", resPacName);
            if (backAndMaskAndFront[2] != null)
                intresiconfront = themeRes.getIdentifier(backAndMaskAndFront[2], "drawable", resPacName);
        }
    }

    BitmapFactory.Options uniformOptions = new BitmapFactory.Options();
    uniformOptions.inScaled = false;
    uniformOptions.inDither = false;
    uniformOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;

    Canvas origCanv;
    Canvas canvas;
    scaleFactor = ThemeTools.getScaleFactor(themeRes, resPacName);
    Bitmap back = null;
    Bitmap mask = null;
    Bitmap front = null;
    Bitmap scaledBitmap = null;
    Bitmap scaledOrig = null;
    Bitmap orig = null;

    if (resPacName.compareTo("") != 0 && themeRes != null) {
        try {
            if (intresiconback != 0)
                back = BitmapFactory.decodeResource(themeRes, intresiconback, uniformOptions);
        } catch (Exception e) {
        }
        try {
            if (intresiconmask != 0)
                mask = BitmapFactory.decodeResource(themeRes, intresiconmask, uniformOptions);
        } catch (Exception e) {
        }
        try {
            if (intresiconfront != 0)
                front = BitmapFactory.decodeResource(themeRes, intresiconfront, uniformOptions);
        } catch (Exception e) {
        }
    }
    //theming vars-----------------------------------------------
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = false;
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    options.inDither = true;

    for (int i = 0; i < shortcuts.size(); i++) {
        if (themeRes != null) {
            iconResource = null;
            intres = 0;
            iconResource = ThemeTools.getResourceName(themeRes, resPacName,
                    shortcuts.get(i).getTargetComponent().toString());
            if (iconResource != null) {
                intres = themeRes.getIdentifier(iconResource, "drawable", resPacName);
            }

            if (intres != 0) {//has single drawable for app
                shortcuts.get(i).setIcon(BitmapFactory.decodeResource(themeRes, intres, uniformOptions));
            } else {

                Drawable drawable = Utilities.createIconDrawable(shortcuts.get(i).getIcon(mIconCache));
                orig = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                        Bitmap.Config.ARGB_8888);
                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
                drawable.draw(new Canvas(orig));

                scaledOrig = Bitmap.createBitmap(ICONSIZE, ICONSIZE, Bitmap.Config.ARGB_8888);
                scaledBitmap = Bitmap.createBitmap(ICONSIZE, ICONSIZE, Bitmap.Config.ARGB_8888);
                canvas = new Canvas(scaledBitmap);
                if (back != null) {
                    canvas.drawBitmap(back, Tools.getResizedMatrix(back, ICONSIZE, ICONSIZE), p);
                }

                origCanv = new Canvas(scaledOrig);
                orig = Tools.getResizedBitmap(orig, ((int) (ICONSIZE * scaleFactor)),
                        ((int) (ICONSIZE * scaleFactor)));
                origCanv.drawBitmap(orig,
                        scaledOrig.getWidth() - (orig.getWidth() / 2) - scaledOrig.getWidth() / 2,
                        scaledOrig.getWidth() - (orig.getWidth() / 2) - scaledOrig.getWidth() / 2, origP);

                if (mask != null) {
                    origCanv.drawBitmap(mask, Tools.getResizedMatrix(mask, ICONSIZE, ICONSIZE), maskp);
                }

                if (back != null) {
                    canvas.drawBitmap(Tools.getResizedBitmap(scaledOrig, ICONSIZE, ICONSIZE), 0, 0, p);
                } else
                    canvas.drawBitmap(Tools.getResizedBitmap(scaledOrig, ICONSIZE, ICONSIZE), 0, 0, p);

                if (front != null)
                    canvas.drawBitmap(front, Tools.getResizedMatrix(front, ICONSIZE, ICONSIZE), p);

                shortcuts.get(i).setIcon(scaledBitmap);
            }
        }
    }
}