Android launcher source code explanation 01: Ui layout explanation 02 -- get the Application List

Source: Internet
Author: User

This series of articles will start the android lancher source code analysis. The example is the launcher3 source code that comes with Android 2.3. It is a http://download.csdn.net/detail/xianming01/4383598

In the previous article "android launcher source code parsing 01: Ui layout details 1", we introduced the theme UI layout in launcher3. In this article, we will begin to introduce some components.

This section describes how to obtain the Application List.

1. layout File
The interface of this part is shown as follows:


It consists of an Application List and a home button.

In the layout file launcher. xml of lancher, the section about the Application List is:

<include layout="@layout/all_apps" />  

The layout file all_mirror.xml is actually contained in the following content:

<merge xmlns:android="http://schemas.android.com/apk/res/android">    <include layout="@layout/all_apps_2d" /></merge>

Its function is to include the layout file all_apps_2d.xml with the following content:

<com.android.launcher3.AllApps2D    xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/all_apps_view"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:padding="2dip"    >    <GridView android:id="@+id/all_apps_2d_grid"        android:tag="all_apps_2d_grid"        android:scrollbars="none"        android:drawSelectorOnTop="false"        android:listSelector="@drawable/grid_selector"        android:verticalSpacing="10dip"        android:numColumns="6"        android:fadingEdgeLength="48dip"        android:cacheColorHint="#FF000000"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_alignParentRight="true"        android:layout_marginRight="@dimen/button_bar_height_portrait"        android:nextFocusRight="@+id/all_apps_2d_home"        android:nextFocusUp="@null"        android:nextFocusLeft="@null"        android:nextFocusDown="@null"        />    <view        class="com.android.launcher3.AllApps2D$HomeButton"        android:id="@+id/all_apps_2d_home"        android:tag="all_apps_2d_home"        android:src="@drawable/home_button"        android:background="#FF000000"        android:layout_alignParentRight="true"        android:layout_centerVertical="true"        android:layout_height="wrap_content"        android:layout_width="@dimen/button_bar_height_portrait"        android:paddingBottom="@dimen/status_bar_height"        android:nextFocusLeft="@+id/all_apps_2d_grid"        android:nextFocusDown="@null"        android:nextFocusUp="@null"        android:nextFocusRight="@null"        /></com.android.launcher3.AllApps2D>

We can see from the above that the implementation of this part is to display the Application List by a gridview, while the Home button is a view.

2. Implementation Principle

In the class all1_2d. Java, we can see the implementation of this part. In fact, in my blog "android ",
The basic knowledge of launcher contains a simple form of this part. If you are interested, you can take a look at it. It is relatively simple.
How is it implemented in the code? Let's look at it:

mHandleView = (HandleView) findViewById(R.id.all_apps_button);          mHandleView.setLauncher(this);          mHandleView.setOnClickListener(this);          mHandleView.setOnLongClickListener(this); 

Let's look at the onclick () method: In The onclick method, there is a code

else if (v == mHandleView) {              if (isAllAppsVisible()) {                  closeAllApps(true);              } else {                  showAllApps(true);              }  

Let's look at the showallapps () method.

void showAllApps(boolean animated) {          mAllAppsGrid.zoom(1.0f, animated);            ((View) mAllAppsGrid).setFocusable(true);          ((View) mAllAppsGrid).requestFocus();                    // TODO: fade these two too          mDeleteZone.setVisibility(View.GONE);          mHandleView.setVisibility(View.GONE);          mPreviousView.setVisibility(View.GONE);          mNextView.setVisibility(View.GONE);      hotseatLeft.setVisibility(View.GONE);      hotseatRight.setVisibility(View.GONE);      }  

Mall1_grid is used above. Let's look for this control:

private AllAppsView mAllAppsGrid;            mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);          mAllAppsGrid.setLauncher(this);          mAllAppsGrid.setDragController(dragController);          ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.          // Manage focusability manually since this thing is always visible          ((View) mAllAppsGrid).setFocusable(false);  

The above code is defined in the setupview () method in launcher. java.
All_apps_view is defined in all_app_2d.xml.
In allapp2d. Java, you can find the methods used by mallappsgrid, such as addapps, removeapps, and zoom:

public void addApps(ArrayList<ApplicationInfo> list) {  //        Log.d(TAG, "addApps: " + list.size() + " apps: " + list.toString());            final int N = list.size();            for (int i=0; i<N; i++) {              final ApplicationInfo item = list.get(i);              int index = Collections.binarySearch(mAllAppsList, item,                      LauncherModel.APP_NAME_COMPARATOR);              if (index < 0) {                  index = -(index+1);              }              mAllAppsList.add(index, item);          }          mAppsAdapter.notifyDataSetChanged();      }        public void removeApps(ArrayList<ApplicationInfo> list) {          final int N = list.size();          for (int i=0; i<N; i++) {              final ApplicationInfo item = list.get(i);              int index = findAppByComponent(mAllAppsList, item);              if (index >= 0) {                  mAllAppsList.remove(index);              } else {                  Log.w(TAG, "couldn't find a match for item \"" + item + "\"");                  // Try to recover.  This should keep us from crashing for now.              }          }          mAppsAdapter.notifyDataSetChanged();      }         public void zoom(float zoom, boolean animate) {  //        Log.d(TAG, "zooming " + ((zoom == 1.0) ? "open" : "closed"));          cancelLongPress();            mZoom = zoom;            if (isVisible()) {              getParent().bringChildToFront(this);              setVisibility(View.VISIBLE);              mGrid.setAdapter(mAppsAdapter);              if (animate) {                  startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.all_apps_2d_fade_in));              } else {                  onAnimationEnd();              }          } else {              if (animate) {                  startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.all_apps_2d_fade_out));              } else {                  onAnimationEnd();              }          }      }  

The zoom () method contains mgrid. setadapter (mappsadapter). In the constructor, assign a value to the adapter.

public AllApps2D(Context context, AttributeSet attrs) {          super(context, attrs);          setVisibility(View.GONE);          setSoundEffectsEnabled(false);            mAppsAdapter = new AppsAdapter(getContext(), mAllAppsList);          mAppsAdapter.setNotifyOnChange(false);      }  

How does one obtain the Application List? Let's go back to launcher. Java, in loadhotseats. Because the function is complex, we will not display it completely, only show the more important parts:

PackageManager pm = getPackageManager();  ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);  List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);  

This will be obtained. The following is a simple example to get the list of programs installed on the SD card:

private void getSdcardApps(){          mSdcardAppsList.clear();          ActivityManager am = (ActivityManager)mLauncher.getSystemService(Activity.ACTIVITY_SERVICE);          PackageManager pm =mLauncher.getPackageManager();            List<android.content.pm.ApplicationInfo> list = pm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);          for (android.content.pm.ApplicationInfo appInfo : list) {                  if((appInfo.flags & android.content.pm.ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0){                      for (ApplicationInfo applicationInfo : mAllAppsList) {                          if(appInfo.packageName.equals(applicationInfo.componentName.getPackageName())){                              mSdcardAppsList.add(applicationInfo);                              break;                          }                      }                  }          }          mAppsAdapter = new AppsAdapter(getContext(), mSdcardAppsList);          mAppsAdapter.notifyDataSetChanged();          mGrid.setAdapter(mAppsAdapter);          text.setVisibility(View.VISIBLE);          text.setBackgroundResource(R.drawable.tab_mmenu_b3_normal);      }  

The above is the parsing of the UI layout of the Application List. It also involves some important aspects that have not been explained. For example, on some mobile phones, you can slide up and down to view the app, and some view the app through pages. How does this happen? This will be introduced in the next article.

References:

Launcher modification -- get Application List launcher source code parsing


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.