android隐藏某apk主菜单 (一)

最近在弄一个需求,要求硬件支持ATV的话,就将ATV主菜单显示出来,否则不显示ATV主菜单。

查了些资料,最开始在launcher里修改,可以实现此功能。在launcher里修改方法:

当launcher在加载所有apk时,判断硬件是否支持atv,若支持,请查看代码:

这是在2.3.5上修改的:

packages/apps/hylauncher/src/com/android/hylauncher

public ApplicationsAdapter(Context context, ArrayList<ApplicationInfo> apps, AppCatalogueFilter filter) {
		super(context, 0, apps);

		mCatalogueFilter = filter;

		mInflater = LayoutInflater.from(context);
		// HY: Load textcolor and bubble color from theme
		String themePackage = AlmostNexusSettingsHelper.getThemePackageName(
				getContext(), Launcher.THEME_DEFAULT);                      
				
		if (!themePackage.equals(Launcher.THEME_DEFAULT)) {
			Resources themeResources = null;
			try {
				themeResources = getContext().getPackageManager()
						.getResourcesForApplication(themePackage);
			} catch (NameNotFoundException e) {
				// e.printStackTrace();
			}
			if (themeResources != null) {
				int textColorId = themeResources.getIdentifier(
						"drawer_text_color", "color", themePackage);
				if (textColorId != 0) {
					mTextColor = themeResources.getColor(textColorId);
					useThemeTextColor = true;
				}
				mBackground = IconHighlights.getDrawable(getContext(),
						IconHighlights.TYPE_DRAWER);
    			try{
    				themeFont=Typeface.createFromAsset(themeResources.getAssets(), "themefont.ttf");
    			}catch (RuntimeException e) {
					// TODO: handle exception
				}
			}
		}
	}

在上面这个函数中String themePackage = AlmostNexusSettingsHelper.getThemePackageName(
getContext(), Launcher.THEME_DEFAULT); 后加上判断条件:

try {
            if (IfExistFile("/sys/class/atv/nmi/dev")) {
                is_atv_exist = true;
                Log.v(TAG, "is_atv_exist = " + is_atv_exist);
            } else {
                is_atv_exist = false;
                Log.v(TAG, "is_atv_exist = " + is_atv_exist);
            }
            }catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                is_atv_exist = false;
                Log.v(TAG, "the atv is not  exist");
            }
这里用到了一个判断文件是否存在的函数:

private boolean IfExistFile(String fileName) throws FileNotFoundException {
        // TODO Auto-generated method stub
        File file = new File(fileName);
        if (file.exists()) {
            return true;
        } else {
            return false;
        }

    }
下面要找到加载所有apk的地方:
@Override
	public void add(ApplicationInfo info) {
		//check allItems before added. It is a fix for all of the multi-icon issue, but will 
		//lose performance. Anyway, we do not expected to have many applications.
		synchronized (allItems) {
			/*if (!allItems.contains(info)) {
				changed = true;
				allItems.add(info);
				Collections.sort(allItems,new ApplicationInfoComparator());
			}*/
			int count=allItems.size();
			boolean found=false;
            boolean update=false;
			for(int i=0;i<count;i++){
				ApplicationInfo athis=allItems.get(i);
				if(info.intent.getComponent()!=null){
				if(athis.intent.getComponent().flattenToString().equals(
							info.intent.getComponent().flattenToString())){
                        if (0 != athis.title.toString().compareTo(info.title.toString())) {
                            athis.title = info.title;
                            update = true;
                        }
						found=true;
						break;
					}
				}
			}
			if(!found){
				allItems.add(info);
				Collections.sort(allItems,new ApplicationInfoComparator());
				if(SprdFeatureUtils.SPRD_APP_HYLAUNCHER_ICON_SORT){
					HyLauncherFunctionIconSort();
				}
				updateDataSet();
			} else if (update) {
				Collections.sort(allItems,new ApplicationInfoComparator());
				if(SprdFeatureUtils.SPRD_APP_HYLAUNCHER_ICON_SORT){
					HyLauncherFunctionIconSort();
				}
				updateDataSet();
				mUpdateViewCache = true;
			}
		} 
	}
添加上符合判断条件的操作:不让加载此apk菜单:

if(info.intent.getComponent().getClassName().equals("com.example.onekeyclearrecentslist.ClearMainActivity") && !is_atv_exist){
   if(info.intent.getComponent().getClassName().equals("com.nmi.test.test") && !is_atv_exist){
   Log.v(TAG, "dont add the atv application");
       return;
   }

加完之后就是以下的样子:

@Override
	public void add(ApplicationInfo info) {
		//check allItems before added. It is a fix for all of the multi-icon issue, but will 
		//lose performance. Anyway, we do not expected to have many applications.
		synchronized (allItems) {
			/*if (!allItems.contains(info)) {
				changed = true;
				allItems.add(info);
				Collections.sort(allItems,new ApplicationInfoComparator());
			}*/
			int count=allItems.size();
			boolean found=false;
            boolean update=false;
			for(int i=0;i<count;i++){
				ApplicationInfo athis=allItems.get(i);
				if(info.intent.getComponent()!=null){
				if(info.intent.getComponent().getClassName().equals("com.example.onekeyclearrecentslist.ClearMainActivity") && !is_atv_exist){
				    if(info.intent.getComponent().getClassName().equals("com.nmi.test.test") && !is_atv_exist){
				    Log.v(TAG, "dont add the atv application");
				        return;
				    }else if(athis.intent.getComponent().flattenToString().equals(
							info.intent.getComponent().flattenToString())){
                        if (0 != athis.title.toString().compareTo(info.title.toString())) {
                            athis.title = info.title;
                            update = true;
                        }
						found=true;
						break;
					}
				}
			}
			if(!found){
				allItems.add(info);
				Collections.sort(allItems,new ApplicationInfoComparator());
				if(SprdFeatureUtils.SPRD_APP_HYLAUNCHER_ICON_SORT){
					HyLauncherFunctionIconSort();
				}
				updateDataSet();
			} else if (update) {
				Collections.sort(allItems,new ApplicationInfoComparator());
				if(SprdFeatureUtils.SPRD_APP_HYLAUNCHER_ICON_SORT){
					HyLauncherFunctionIconSort();
				}
				updateDataSet();
				mUpdateViewCache = true;
			}
		} 
	}
编译此模块,push到手机里,验证是否还会有atv菜单。
相关文章
相关标签/搜索