본문 바로가기

Linux

홈스크린 특정 위치에 디폴트 숏컷 아이콘 만들기 (default shortcut icon )

package/app/에 있는 런처 어플리케이션을 수정한다.

Launcher.java에서 onCreate부분을 수정한다.

        mSavedState = savedInstanceState;
        restoreState(mSavedState);
       
       
        SharedPreferences pref = getSharedPreferences("Launcher_ShortCut", Activity.MODE_PRIVATE);
        boolean bShortCut_Customize = pref.getBoolean("ShortCut_Customize", false);
        if(!bShortCut_Customize) {
            SharedPreferences.Editor ed = pref.edit();
            ed.putBoolean("ShortCut_Customize", true);
            ed.commit();
            removeShortcut("com.android.music");
            removeShortcut("com.android.videoplayer");
                  
            insertShortcut("itectokyo.wfd","Miracast",1,4);
            insertShortcut("com.google.android.gallery3d","Gallery",2,4);
            insertShortcut("com.google.android.music","Play Music",3,4);
            insertShortcut("com.google.android.videos","Play Movies & TV",2,0);
            insertShortcut("com.android.vending","Play Store",3,0);
            insertShortcut("com.welgate.welshare","WelShare",4,0);
        }


      void insertShortcut(String packagename, String name, int cell_x, int cell_y) {
        Log.d("Launcher","insertShortcut packagename : " +packagename );
        try {
           
            Intent scIntent = new Intent(Intent.ACTION_MAIN);
            PackageManager manager = getPackageManager();
            scIntent = manager.getLaunchIntentForPackage(packagename);
            scIntent.addCategory(Intent.CATEGORY_LAUNCHER);
            scIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                    | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
            Intent sIntent = new Intent(
                    "com.android.launcher.action.INSTALL_SHORTCUT");
            sIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, scIntent);
            PackageInfo pinfo = manager.getPackageInfo(packagename, PackageManager.GET_META_DATA);
            Context otherAppCtx = getApplicationContext().createPackageContext(packagename, CONTEXT_IGNORE_SECURITY);
            Drawable d = otherAppCtx.getResources().getDrawableForDensity(pinfo.applicationInfo.icon, DisplayMetrics.DENSITY_HIGH);
            Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
            String label = (String) manager.getApplicationLabel(pinfo.applicationInfo);
            if(name.equals("Miracast") || name.equals("WelShare")){
                bitmap = Bitmap.createScaledBitmap(bitmap, (int)(bitmap.getWidth()*0.75f), (int)(bitmap.getHeight()*0.75f), true);
            } else if(name.equals("Play Store") || name.equals("Play Music") || name.equals("Play Movies & TV")){
                if(label != null && label.contains("Google")){
                    label = name;
                }
            }
            sIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, label);
            sIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, bitmap);
            sIntent.putExtra("duplicate", false);
           
            int[] mCoordinates = new int[LauncherModel.getCellCountX()*LauncherModel.getCellCountY()];
            if (findEmptyCell(getApplicationContext(), mCoordinates, Launcher.getScreen())) {
                    boolean duplicate = sIntent.getBooleanExtra(Launcher.EXTRA_SHORTCUT_DUPLICATE, true);
                    if (duplicate || !LauncherModel.shortcutExists(getApplicationContext(), label, sIntent)) {
                        LauncherApplication app = (LauncherApplication) getApplicationContext();
                        ShortcutInfo info = app.getModel().addShortcut(getApplicationContext(), sIntent,
                                LauncherSettings.Favorites.CONTAINER_DESKTOP, Launcher.getScreen(), mCoordinates[cell_x],
                                mCoordinates[cell_y], true);
                        Log.d("Launcher","insertShortcut info : " +info );
                    } else {
                        Log.d("Launcher","insertShortcut duplicate : " +duplicate );
                    }
            } else {
                 Log.d("Launcher","insertShortcut not findemptycell" );
            }
        } catch (Exception e) {
            Log.d("Launcher","insertShortcut e : " +e.getMessage() );
        }

    }

    boolean findEmptyCell(Context context, int[] xy, int screen) {
        final int xCount = LauncherModel.getCellCountX();
        final int yCount = LauncherModel.getCellCountY();
        boolean[][] occupied = new boolean[xCount][yCount];
       
        ArrayList<ItemInfo> items = LauncherModel.getItemsInLocalCoordinates(context);
        ItemInfo item = null;
        int cellX, cellY, spanX, spanY;
        for (int i = 0; i < items.size(); ++i) {
            item = items.get(i);
            if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
                if (item.screen == screen) {
                    cellX = item.cellX;
                    cellY = item.cellY;
                    spanX = item.spanX;
                    spanY = item.spanY;
                    for (int x = cellX; x < cellX + spanX && x < xCount; x++) {
                        for (int y = cellY; y < cellY + spanY && y < yCount; y++) {
                            occupied[x][y] = true;
                        }
                    }
                }
            }
        }
        return CellLayout.findVacantCell(xy, 1, 1, xCount, yCount, occupied);
    }
   
    void removeShortcut(String packagename) {
        Log.d("Launcher","removeShortcut packagename : " + packagename );
        String name = "";
        try {
            PackageInfo pinfo = getPackageManager().getPackageInfo(packagename, PackageManager.GET_META_DATA);
            name = (String) getPackageManager().getApplicationLabel(pinfo.applicationInfo);
            if(name != null && name.toString().contains("Google")){
                name = name.toString().replace("Google", "");
            }
           
            final ContentResolver cr = getApplicationContext().getContentResolver();
            Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
                new String[] { LauncherSettings.Favorites._ID, LauncherSettings.Favorites.INTENT },
                LauncherSettings.Favorites.TITLE + "=?", new String[] { name }, null);
   
            final int intentIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
            final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
            boolean changed = false;
            try {
                while (c.moveToNext()) {
                    try {
                        final long id = c.getLong(idIndex);
                        final Uri uri = LauncherSettings.Favorites.getContentUri(id, false);
                        cr.delete(uri, null, null);
                        changed = true;
                    } catch (Exception e) {
                        // Ignore
                    }
                }
            } finally {
                c.close();
            }
            Log.d("Launcher","removeShortcut changed : " +changed);
        }catch(Exception e){
            Log.d("Launcher","removeShortcut e : " +e.getMessage());
        }
    }

 

LauncherModel.java loadWorkspace 함수 수정...

 //수정부분
                SharedPreferences pref = mApp.getSharedPreferences("Launcher_ShortCut", Activity.MODE_PRIVATE);
                boolean bShortCut_Customize = pref.getBoolean("ShortCut_Customize", false);
                boolean bChangePosition = pref.getBoolean("ChangePosition", false);
               

             //--
               
                while (!mStopped && c.moveToNext()) {
                    try {
                        int itemType = c.getInt(itemTypeIndex);

                        switch (itemType) {
                        case LauncherSettings.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,
                                        titleIndex, mLabelCache);
                            } else {
                                info = getShortcutInfo(c, context, iconTypeIndex,
                                        iconPackageIndex, iconResourceIndex, iconIndex,
                                        titleIndex);
                            }

                            if (info != null) {
                                info.intent = intent;
                                info.id = c.getLong(idIndex);
                                container = c.getInt(containerIndex);
                                info.container = container;
                                info.screen = c.getInt(screenIndex);
                                info.cellX = c.getInt(cellXIndex);
                                info.cellY = c.getInt(cellYIndex);
                               
                                //수정부분
                                info = setInfo_Label(info);
                                if(bShortCut_Customize && !bChangePosition) {
                                    info = setInfo_Cell(info,pref);
                                }
                               



public ShortcutInfo setInfo_Label(ShortcutInfo info){
            if(info.title != null){
                Log.d("Launcher","setInfo_Label info.title : " + info.title );
                Log.d("Launcher","setInfo_Label info.cellX : " + info.cellX );
                Log.d("Launcher","setInfo_Label info.cellY : " + info.cellY );
                String packageName = info.intent.getPackage();
                Log.d("Launcher","setInfo_Label packageName : " +packageName);
                if(packageName != null){
                    try{
                        if(packageName.equals("itectokyo.wfd")){
                            PackageInfo pinfo = mApp.getPackageManager().getPackageInfo("itectokyo.wfd", PackageManager.GET_META_DATA);
                            info.title = mApp.getPackageManager().getApplicationLabel(pinfo.applicationInfo);
                        } else if(packageName.equals("com.google.android.gallery3d")){
                            PackageInfo pinfo = mApp.getPackageManager().getPackageInfo("com.google.android.gallery3d", PackageManager.GET_META_DATA);
                            info.title = mApp.getPackageManager().getApplicationLabel(pinfo.applicationInfo);
                        } else if(packageName.equals("com.google.android.music")){
                            PackageInfo pinfo = mApp.getPackageManager().getPackageInfo("com.google.android.music", PackageManager.GET_META_DATA);
                            info.title = mApp.getPackageManager().getApplicationLabel(pinfo.applicationInfo);
                            if(info.title != null && info.title.toString().contains("Google")){
                                info.title = info.title.toString().replace("Google", "");
                            }
                        }else if(packageName.equals("com.google.android.videos")){
                            PackageInfo pinfo = mApp.getPackageManager().getPackageInfo("com.google.android.videos", PackageManager.GET_META_DATA);
                            info.title = mApp.getPackageManager().getApplicationLabel(pinfo.applicationInfo);
                            if(info.title != null && info.title.toString().contains("Google")){
                                info.title = info.title.toString().replace("Google", "");
                            }
                        }else if(packageName.equals("com.android.vending")){
                            PackageInfo pinfo = mApp.getPackageManager().getPackageInfo("com.android.vending", PackageManager.GET_META_DATA);
                            info.title = mApp.getPackageManager().getApplicationLabel(pinfo.applicationInfo);
                            if(info.title != null && info.title.toString().contains("Google")){
                                info.title = info.title.toString().replace("Google", "");
                            }
                        }else if(packageName.equals("com.welgate.welshare")){
                            PackageInfo pinfo = mApp.getPackageManager().getPackageInfo("com.welgate.welshare", PackageManager.GET_META_DATA);
                            info.title = mApp.getPackageManager().getApplicationLabel(pinfo.applicationInfo);
                        }
                        Log.d("Launcher","setInfo_Label after info.title : " +info.title );
                    }catch(Exception e){
                        Log.d("Launcher","setInfo_Label e : " +e.getMessage() );
                    }
                } else {
                    Log.d("Launcher","setInfo_Label packageName is null");
                }
            }
            return info;
        }
        public ShortcutInfo setInfo_Cell(ShortcutInfo info, SharedPreferences pref){
            if(info.title != null){
                Log.d("Launcher","setInfo_Cell info.title : " +info.title );
                Log.d("Launcher","setInfo_Cell info.cellX : " +info.cellX );
                Log.d("Launcher","setInfo_Cell info.cellY : " +info.cellY );
                String packageName = info.intent.getPackage();
                Log.d("Launcher","setInfo_Customize packageName : " +packageName);
                try{
                    if(packageName.equals("itectokyo.wfd")){
                        info.cellX = 1;
                        info.cellY = 4;
                    } else if(packageName.equals("com.google.android.gallery3d")){
                        info.cellX = 2;
                        info.cellY = 4;
                    } else if(packageName.equals("com.google.android.music")){
                        info.cellX = 3;
                        info.cellY = 4;
                    }else if(packageName.equals("com.google.android.videos")){
                        info.cellX = 2;
                        info.cellY = 0;
                    }else if(packageName.equals("com.android.vending")){
                        info.cellX = 3;
                        info.cellY = 0;
                    }else if(packageName.equals("com.welgate.welshare")){
                        info.cellX = 4;
                        info.cellY = 0;
                        SharedPreferences.Editor ed = pref.edit();
                        ed.putBoolean("ChangePosition", true);
                        ed.commit();
                    }
                    updateCellXY(new Long(info.id).toString(), info.cellX, info.cellY);
                }catch(Exception e){
                    Log.d("Launcher","setInfo_Customize e : " +e.getMessage() );
                }
            }
            return info;
        }
        void updateCellXY(String _id, int CellX, int CellY){
            final ContentResolver cr = mApp.getContentResolver();
            Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
                new String[] { LauncherSettings.Favorites._ID, LauncherSettings.Favorites.INTENT },
                LauncherSettings.Favorites._ID + "=?", new String[] { _id }, null);
           
            final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
            try {
               
                while (c.moveToNext()) {
                    try {
                        final long id = c.getLong(idIndex);
                        final Uri uri = LauncherSettings.Favorites.getContentUri(id, false);
                        ContentValues values = new ContentValues();
                        values.put(LauncherSettings.Favorites.CELLX, CellX);
                        values.put(LauncherSettings.Favorites.CELLY, CellY);
                        cr.update(uri, values, null, null);
                    } catch (Exception e) {
                        // Ignore
                    }
                }
            } finally {
                c.close();
            }
        }