Launcher2 add default background to desktop shortcut icon

Source: Internet
Author: User

It takes a lot of time to get a thorough understanding of all of Launcher2 's code, at least for now there is only a need to see the part of the code that needs to be relevant.

A lot of information on the web, to the desktop shortcut icons are joined a unified background map to make its style unified, according to the level of individual appreciation may have good and bad bar.

Look directly at the code.

The first need to see the code must be Launcher.java, the habitual first look at OnCreate

Look at the code process is not a table, straight into the subject to see this content, oncreate of these lines of code

        if (!mrestoring) {            if (spausedfromuseraction) {                //If the user leaves launcher, then we should just load items asy nchronously when                //they return.                Mmodel.startloader (True,-1);            } else {                //We only load the page synchronously if the user rotates (or triggers a                //configuration change) while LA Uncher                is in the foreground Mmodel.startloader (True, Mworkspace.getcurrentpage ());            }        }
Continue to see, Mmodel is the object of Launchermodel, naturally next see Launchermodel.java, actually the function of the surface can be seen as a load related functions, is actually loading the entire desktop process.

Take a look at the complete Startloader function

    public void Startloader (boolean islaunching, int synchronousbindpage) {synchronized (MLock) {if (D            Ebug_loaders) {log.d (TAG, "Startloader islaunching=" + islaunching); }//Clear any deferred bind-runnables from the synchronized load process//We must does this before            Any loading/binding is scheduled below.            Mdeferredbindrunnables.clear (); Don ' t bother to start the thread if we know it's not going to do anything if (mcallbacks! = null &&                Mcallbacks.get ()! = NULL) {//If there is already one running, tell it to stop. Also, don ' t downgrade islaunching if we ' re already running islaunching = islaunching | |                Stoploaderlocked ();                Mloadertask = new Loadertask (mApp, islaunching); if (Synchronousbindpage >-1 && mallappsloaded && mworkspaceloaded) {mloadertask.ru NbindsYnchronouspage (Synchronousbindpage);                    } else {sworkerthread.setpriority (thread.norm_priority);                Sworker.post (Mloadertask); }            }        }    }
What you need to be aware of in this code is an asynchronous load of the operation interface Loadtask, continue to see the core code of the Loadtask method

        private void Loadandbindworkspace () {            misloadingandbindingworkspace = true;            Load the workspace            if (debug_loaders) {                log.d (TAG, "Loadandbindworkspace mworkspaceloaded=" + mworkspaceloaded);            }            if (!mworkspaceloaded) {                loadworkspace ();                Synchronized (loadertask.this) {                    if (mstopped) {                        return;                    }                    mworkspaceloaded = true;                }            }            Bind the workspace            bindworkspace ( -1);        }
The above Code section is one of these functions, which can be known by name as a function of loading and binding workspace.

Keep looking at the Loadworkspace function.

Because this function code is more temporary intercept some useful paste out

............ Shortcutinfo info, ... switch (itemType) {case Launchersetti. Ngs.                                Favorites.ITEM_TYPE_APPLICATION:case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:                                Intentdescription = c.getstring (Intentindex);                                try {intent = Intent.parseuri (intentdescription, 0);                                } catch (URISyntaxException e) {continue;                                    } if (ItemType = = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) { info = getshortcutinfo (Manager, intent, context, C, IconIndex, TI                                Tleindex, Mlabelcache);                     } else {info = Getshortcutinfo (c, Context, Icontypeindex,                       Iconpackageindex, Iconresourceindex, IconIndex, Titleind ex); .....
Focus on the Shortcutinfo class, which defines all the information contained in a shortcut icon, which contains the icon information for the shortcut. Focus on the Getshortcutinfo function.

.......        if (resolveInfo! = null) {            icon = Miconcache.geticon (ComponentName, ResolveInfo, Labelcache);        }        The DB        if (icon = = null) {            if (c! = null) {                icon = geticonfromcursor (c, iconindex, context);            }        }        //The fallback icon        if (icon = = null) {            icon = Getfallbackicon ();            Info.usingfallbackicon = true;        }        Info.seticon (icon);
The focus of this change appears, you can see the icon of the way the acquisition has appeared, you can see the method will be three specific ways to get. Miconcache.geticon () and Geticonfromcursor (), respectively, Getfallbackicon ()

Look at these two functions, the first is GetIcon this function belongs to Iconcache.java, directly look at the function code

 public Bitmap GetIcon (componentname component, ResolveInfo ResolveInfo, Hashmap<object, charsequence> Labelcache            {synchronized (Mcache) {if (ResolveInfo = = NULL | | component = = NULL) {return null;            } CacheEntry Entry = cachelocked (component, ResolveInfo, Labelcache);        return Entry.icon; }} ... public Bitmap GetIcon (Intent Intent) {synchronized (Mcache) {final ResolveInfo Reso            Lveinfo = mpackagemanager.resolveactivity (Intent, 0);            ComponentName component = Intent.getcomponent ();            if (ResolveInfo = = NULL | | component = = NULL) {return mdefaulticon;            } CacheEntry Entry = cachelocked (component, resolveInfo, NULL);        return Entry.icon; }    }
Then look at the Geticonfromcursor () function and parse it later.

    Bitmap Geticonfromcursor (Cursor c, int iconindex, context context) {        @SuppressWarnings ("all")//Suppress Dead code WA Rning        Final Boolean debug = false;        if (Debug) {            log.d (TAG, "Geticonfromcursor app="                    + c.getstring (C.getcolumnindexorthrow ( LauncherSettings.Favorites.TITLE)));        }        byte[] data = C.getblob (IconIndex);        try {            return Utilities.createiconbitmap (                    bitmapfactory.decodebytearray (data, 0, data.length), context);        } catch (Exception e) {            return null;        }    }
Since the return type of the above two functions is known to return bitmap, it is certain that the bitmap returned at this time is the icon of the app shown behind us in workspace.

It is also possible to further analyze these two functions, and their valid return values are still functions cachelocked () and Utilities.createiconbitmap () respectively. Then we can make sure that the application icons we get are all done in the above two functions, and then continue to look at these two functions.

    Private CacheEntry cachelocked (componentname componentname, ResolveInfo info, hashmap<object, Charsequen        Ce> labelcache) {CacheEntry entry = Mcache.get (componentname);            if (entry = = null) {entry = new cacheentry ();            Mcache.put (componentname, entry);            ComponentName key = Launchermodel.getcomponentnamefromresolveinfo (info); if (Labelcache! = null && Labelcache.containskey (key)) {Entry.title = Labelcache.get (key). toString            ();                } else {entry.title = Info.loadlabel (Mpackagemanager). toString ();                if (Labelcache! = null) {Labelcache.put (key, Entry.title);            }} if (entry.title = = null) {entry.title = Info.activityInfo.name;        } Entry.icon = Utilities.createiconbitmap (Getfullresicon (info), mcontext);    } return entry; }
This function is the application of the icon from the cache and as the final display of the process of the icon, then we can naturally complete the background of the icon to add, and then return to the background of the app icon.

Specific practices are as follows to modify cachelocked as an example:

    Private CacheEntry cachelocked (componentname componentname, ResolveInfo info, hashmap<object, Charsequen        Ce> labelcache) {CacheEntry entry = Mcache.get (componentname);            if (entry = = null) {entry = new cacheentry ();            Mcache.put (componentname, entry);            ComponentName key = Launchermodel.getcomponentnamefromresolveinfo (info); if (Labelcache! = null && Labelcache.containskey (key)) {Entry.title = Labelcache.get (key). toString            ();                } else {entry.title = Info.loadlabel (Mpackagemanager). toString ();                if (Labelcache! = null) {Labelcache.put (key, Entry.title);            }} if (entry.title = = null) {entry.title = Info.activityInfo.name;            }//add icon background Bitmap background = null; Bitmap temp = Utilities.createiconbitmap (Getfullresicon (info), McontexT);            Entry.icon = Createcompoundbitmap (background,temp);        /* Entry.icon = Utilities.createiconbitmap (Getfullresicon (info), mcontext); */}    return entry;        } public Bitmap Createcompoundbitmap (Bitmap bg, Bitmap icon) {int bgwidth = Bg.getwidth ();                    int bgheight = Bg.getheight ();                    icon = SCALEBITMAP2 (icon,0.8f,0.8f);            Final int iconwidth = Icon.getwidth ();            Final int iconheight = Icon.getheight ();            Bitmap Compoundbitmap = Bitmap.createbitmap (Bgwidth, Bgheight, config.argb_8888);            Canvas canvas = new canvas (COMPOUNDBITMAP);            Canvas.drawbitmap (BG, 0, 0, NULL);            Canvas.drawbitmap (icon, (bgwidth-iconwidth)/2, (Bgheight-iconheight)/2, NULL);            Canvas.save (Canvas.all_save_flag);                        Canvas.restore ();    return compoundbitmap;} Static Bitmap ScaleBitmap2 (Bitmap BM, float SX, float sy) {if (sx = = 1.0f && sy = = 1.0f) {return BM;        } Matrix Matrix = new Matrix ();        Matrix.postscale (SX, SY); Return Bitmap.createbitmap (BM, 0, 0, bm.getwidth (), Bm.getheight (), Matrix, True);}
The above code can be unified to all icons set the background, of course, you can also set a background map for all icons, nothing more than to add a number of random numbers and then judge with the background map, do not show here. Because it is easy to achieve the effect, it is not sent, as long as the code to modify the results can be seen.

Another way is to modify the Createiconbitmap (drawable icon, context context) function in Utilities.java.

The practice is the same, mainly to give the icon to add the background and return to the bitmap to display the re-composition. In this function, it is simple to modify the two functions that have just been modified separately, and the code is not affixed to the function.

Finally, the difference between these two functions is simply Personal opinion is Miconcache.geticon. The function that gets the icon in Getshortcutinfo is the first method to get the application icon in the case where the application information exists, so you can guess from the name that it should be an icon that already exists in the cache, and another GE Ticonfromcursor This function is retrieved from the saved database to extract the icon directly by the application. The two approaches achieve the same purpose, nothing more than a more thoughtful approach, and there is little difference between the requirements to achieve a unified background, both of which should be possible. I did it at Iconcache for the time being, because I did not find the anomaly and then I did not go on trying. The students who need to study can continue to study the content that needs to be written today. Record for later debugging.

Launcher2 add default background to desktop shortcut icon

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.