Exemplo n.º 1
0
 public static AdapterItem asSectionBreak(int pos, SectionInfo section) {
   AdapterItem item = new AdapterItem();
   item.viewType = AllAppsGridAdapter.SECTION_BREAK_VIEW_TYPE;
   item.position = pos;
   item.sectionInfo = section;
   section.sectionBreakItem = item;
   return item;
 }
Exemplo n.º 2
0
  /** Merges multiple sections to reduce visual raggedness. */
  private void mergeSections() {
    // Ignore merging until we have an algorithm and a valid row size
    if (mMergeAlgorithm == null || mNumAppsPerRow == 0) {
      return;
    }

    // Go through each section and try and merge some of the sections
    if (!hasFilter()) {
      int sectionAppCount = 0;
      for (int i = 0; i < mSections.size() - 1; i++) {
        SectionInfo section = mSections.get(i);
        sectionAppCount = section.numApps;
        int mergeCount = 1;

        // Merge rows based on the current strategy
        while (i < (mSections.size() - 1)
            && mMergeAlgorithm.continueMerging(
                section, mSections.get(i + 1), sectionAppCount, mNumAppsPerRow, mergeCount)) {
          SectionInfo nextSection = mSections.remove(i + 1);

          // Remove the next section break
          mAdapterItems.remove(nextSection.sectionBreakItem);
          int pos = mAdapterItems.indexOf(section.firstAppItem);

          // Point the section for these new apps to the merged section
          int nextPos = pos + section.numApps;
          for (int j = nextPos; j < (nextPos + nextSection.numApps); j++) {
            AdapterItem item = mAdapterItems.get(j);
            item.sectionInfo = section;
            item.sectionAppIndex += section.numApps;
          }

          // Update the following adapter items of the removed section item
          pos = mAdapterItems.indexOf(nextSection.firstAppItem);
          for (int j = pos; j < mAdapterItems.size(); j++) {
            AdapterItem item = mAdapterItems.get(j);
            item.position--;
          }
          section.numApps += nextSection.numApps;
          sectionAppCount += nextSection.numApps;

          if (DEBUG) {
            Log.d(
                TAG,
                "Merging: "
                    + nextSection.firstAppItem.sectionName
                    + " to "
                    + section.firstAppItem.sectionName
                    + " mergedNumRows: "
                    + (sectionAppCount / mNumAppsPerRow));
          }
          mergeCount++;
        }
      }
    }
  }
Exemplo n.º 3
0
 public static AdapterItem asPredictedApp(
     int pos,
     SectionInfo section,
     String sectionName,
     int sectionAppIndex,
     AppInfo appInfo,
     int appIndex) {
   AdapterItem item = asApp(pos, section, sectionName, sectionAppIndex, appInfo, appIndex);
   item.viewType = AllAppsGridAdapter.PREDICTION_ICON_VIEW_TYPE;
   return item;
 }
Exemplo n.º 4
0
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    // Log.d("ddd", "getView");
    if (mInflater == null) {
      mInflater = LayoutInflater.from(parent.getContext());
    }

    AdapterItem<T> item;
    if (convertView == null) {
      item = getItemView(mType);
      convertView = mInflater.inflate(item.getLayoutResId(), parent, false);
      convertView.setTag(R.id.tag_item, item);
      item.onBindViews(convertView);
      item.onSetViews();
      if (DEBUG) convertView.setBackgroundColor(0xffff0000);
    } else {
      item = (AdapterItem<T>) convertView.getTag(R.id.tag_item);
      if (DEBUG) convertView.setBackgroundColor(0xff00ff00);
    }
    item.onUpdateViews(mDataList.get(position), position);
    return convertView;
  }
Exemplo n.º 5
0
 public static AdapterItem asApp(
     int pos,
     SectionInfo section,
     String sectionName,
     int sectionAppIndex,
     AppInfo appInfo,
     int appIndex) {
   AdapterItem item = new AdapterItem();
   item.viewType = AllAppsGridAdapter.ICON_VIEW_TYPE;
   item.position = pos;
   item.sectionInfo = section;
   item.sectionName = sectionName;
   item.sectionAppIndex = sectionAppIndex;
   item.appInfo = appInfo;
   item.appIndex = appIndex;
   return item;
 }
Exemplo n.º 6
0
  /**
   * Updates the set of filtered apps with the current filter. At this point, we expect
   * mCachedSectionNames to have been calculated for the set of all apps in mApps.
   */
  private void updateAdapterItems() {
    SectionInfo lastSectionInfo = null;
    String lastSectionName = null;
    FastScrollSectionInfo lastFastScrollerSectionInfo = null;
    int position = 0;
    int appIndex = 0;

    // Prepare to update the list of sections, filtered apps, etc.
    mFilteredApps.clear();
    mFastScrollerSections.clear();
    mAdapterItems.clear();
    mSections.clear();

    if (DEBUG_PREDICTIONS) {
      if (mPredictedAppComponents.isEmpty() && !mApps.isEmpty()) {
        mPredictedAppComponents.add(
            new ComponentKey(mApps.get(0).componentName, UserHandleCompat.myUserHandle()));
        mPredictedAppComponents.add(
            new ComponentKey(mApps.get(0).componentName, UserHandleCompat.myUserHandle()));
        mPredictedAppComponents.add(
            new ComponentKey(mApps.get(0).componentName, UserHandleCompat.myUserHandle()));
        mPredictedAppComponents.add(
            new ComponentKey(mApps.get(0).componentName, UserHandleCompat.myUserHandle()));
      }
    }

    // Process the predicted app components
    mPredictedApps.clear();
    if (mPredictedAppComponents != null && !mPredictedAppComponents.isEmpty() && !hasFilter()) {
      for (ComponentKey ck : mPredictedAppComponents) {
        AppInfo info = mComponentToAppMap.get(ck);
        if (info != null) {
          mPredictedApps.add(info);
        } else {
          if (LauncherAppState.isDogfoodBuild()) {
            Log.e(TAG, "Predicted app not found: " + ck.flattenToString(mLauncher));
          }
        }
        // Stop at the number of predicted apps
        if (mPredictedApps.size() == mNumPredictedAppsPerRow) {
          break;
        }
      }

      if (!mPredictedApps.isEmpty()) {
        // Add a section for the predictions
        lastSectionInfo = new SectionInfo();
        lastFastScrollerSectionInfo = new FastScrollSectionInfo("");
        AdapterItem sectionItem = AdapterItem.asSectionBreak(position++, lastSectionInfo);
        mSections.add(lastSectionInfo);
        mFastScrollerSections.add(lastFastScrollerSectionInfo);
        mAdapterItems.add(sectionItem);

        // Add the predicted app items
        for (AppInfo info : mPredictedApps) {
          AdapterItem appItem =
              AdapterItem.asPredictedApp(
                  position++, lastSectionInfo, "", lastSectionInfo.numApps++, info, appIndex++);
          if (lastSectionInfo.firstAppItem == null) {
            lastSectionInfo.firstAppItem = appItem;
            lastFastScrollerSectionInfo.fastScrollToItem = appItem;
          }
          mAdapterItems.add(appItem);
          mFilteredApps.add(info);
        }
      }
    }

    // Recreate the filtered and sectioned apps (for convenience for the grid layout) from the
    // ordered set of sections
    for (AppInfo info : getFiltersAppInfos()) {
      String sectionName = getAndUpdateCachedSectionName(info.title);

      // Create a new section if the section names do not match
      if (lastSectionInfo == null || !sectionName.equals(lastSectionName)) {
        lastSectionName = sectionName;
        lastSectionInfo = new SectionInfo();
        lastFastScrollerSectionInfo = new FastScrollSectionInfo(sectionName);
        mSections.add(lastSectionInfo);
        mFastScrollerSections.add(lastFastScrollerSectionInfo);

        // Create a new section item to break the flow of items in the list
        if (!hasFilter()) {
          AdapterItem sectionItem = AdapterItem.asSectionBreak(position++, lastSectionInfo);
          mAdapterItems.add(sectionItem);
        }
      }

      // Create an app item
      AdapterItem appItem =
          AdapterItem.asApp(
              position++,
              lastSectionInfo,
              sectionName,
              lastSectionInfo.numApps++,
              info,
              appIndex++);
      if (lastSectionInfo.firstAppItem == null) {
        lastSectionInfo.firstAppItem = appItem;
        lastFastScrollerSectionInfo.fastScrollToItem = appItem;
      }
      mAdapterItems.add(appItem);
      mFilteredApps.add(info);
    }

    // Append the search market item if we are currently searching
    if (hasFilter()) {
      if (hasNoFilteredResults()) {
        mAdapterItems.add(AdapterItem.asEmptySearch(position++));
      } else {
        mAdapterItems.add(AdapterItem.asDivider(position++));
      }
      mAdapterItems.add(AdapterItem.asMarketSearch(position++));
    }

    // Merge multiple sections together as requested by the merge strategy for this device
    mergeSections();

    if (mNumAppsPerRow != 0) {
      // Update the number of rows in the adapter after we do all the merging (otherwise, we
      // would have to shift the values again)
      int numAppsInSection = 0;
      int numAppsInRow = 0;
      int rowIndex = -1;
      for (AdapterItem item : mAdapterItems) {
        item.rowIndex = 0;
        if (item.viewType == AllAppsGridAdapter.SECTION_BREAK_VIEW_TYPE) {
          numAppsInSection = 0;
        } else if (item.viewType == AllAppsGridAdapter.ICON_VIEW_TYPE
            || item.viewType == AllAppsGridAdapter.PREDICTION_ICON_VIEW_TYPE) {
          if (numAppsInSection % mNumAppsPerRow == 0) {
            numAppsInRow = 0;
            rowIndex++;
          }
          item.rowIndex = rowIndex;
          item.rowAppIndex = numAppsInRow;
          numAppsInSection++;
          numAppsInRow++;
        }
      }
      mNumAppRowsInAdapter = rowIndex + 1;

      // Pre-calculate all the fast scroller fractions
      switch (mFastScrollDistributionMode) {
        case FAST_SCROLL_FRACTION_DISTRIBUTE_BY_ROWS_FRACTION:
          float rowFraction = 1f / mNumAppRowsInAdapter;
          for (FastScrollSectionInfo info : mFastScrollerSections) {
            AdapterItem item = info.fastScrollToItem;
            if (item.viewType != AllAppsGridAdapter.ICON_VIEW_TYPE
                && item.viewType != AllAppsGridAdapter.PREDICTION_ICON_VIEW_TYPE) {
              info.touchFraction = 0f;
              continue;
            }

            float subRowFraction = item.rowAppIndex * (rowFraction / mNumAppsPerRow);
            info.touchFraction = item.rowIndex * rowFraction + subRowFraction;
          }
          break;
        case FAST_SCROLL_FRACTION_DISTRIBUTE_BY_NUM_SECTIONS:
          float perSectionTouchFraction = 1f / mFastScrollerSections.size();
          float cumulativeTouchFraction = 0f;
          for (FastScrollSectionInfo info : mFastScrollerSections) {
            AdapterItem item = info.fastScrollToItem;
            if (item.viewType != AllAppsGridAdapter.ICON_VIEW_TYPE
                && item.viewType != AllAppsGridAdapter.PREDICTION_ICON_VIEW_TYPE) {
              info.touchFraction = 0f;
              continue;
            }
            info.touchFraction = cumulativeTouchFraction;
            cumulativeTouchFraction += perSectionTouchFraction;
          }
          break;
      }
    }

    // Refresh the recycler view
    if (mAdapter != null) {
      mAdapter.notifyDataSetChanged();
    }
  }
Exemplo n.º 7
0
 public static AdapterItem asMarketSearch(int pos) {
   AdapterItem item = new AdapterItem();
   item.viewType = AllAppsGridAdapter.SEARCH_MARKET_VIEW_TYPE;
   item.position = pos;
   return item;
 }
Exemplo n.º 8
0
 public static AdapterItem asDivider(int pos) {
   AdapterItem item = new AdapterItem();
   item.viewType = AllAppsGridAdapter.SEARCH_MARKET_DIVIDER_VIEW_TYPE;
   item.position = pos;
   return item;
 }
Exemplo n.º 9
0
 public static AdapterItem asEmptySearch(int pos) {
   AdapterItem item = new AdapterItem();
   item.viewType = AllAppsGridAdapter.EMPTY_SEARCH_VIEW_TYPE;
   item.position = pos;
   return item;
 }