public View getView(int position, View convertView, ViewGroup parent) {
      // A ViewHolder keeps references to children views to avoid unnecessary calls
      // to findViewById() on each row.
      AppViewHolder holder =
          AppViewHolder.createOrRecycle(mManageApplications.mInflater, convertView);
      convertView = holder.rootView;

      // Bind the data efficiently with the holder
      ApplicationsState.AppEntry entry = mEntries.get(position);
      synchronized (entry) {
        holder.entry = entry;
        if (entry.label != null) {
          holder.appName.setText(entry.label);
        }
        mState.ensureIcon(entry);
        if (entry.icon != null) {
          holder.appIcon.setImageDrawable(entry.icon);
        }
        updateSummary(holder);
        if ((entry.info.flags & ApplicationInfo.FLAG_INSTALLED) == 0) {
          holder.disabled.setVisibility(View.VISIBLE);
          holder.disabled.setText(R.string.not_installed);
        } else if (!entry.info.enabled) {
          holder.disabled.setVisibility(View.VISIBLE);
          holder.disabled.setText(R.string.disabled);
        } else {
          holder.disabled.setVisibility(View.GONE);
        }
      }
      mActive.remove(convertView);
      mActive.add(convertView);
      convertView.setEnabled(isEnabled(position));
      return convertView;
    }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    mApplicationsState = ApplicationsState.getInstance(getActivity().getApplication());

    Intent intent = getActivity().getIntent();
    Bundle args = getArguments();
    String className = args != null ? args.getString(EXTRA_CLASSNAME) : null;
    if (className == null) {
      className = intent.getComponent().getClassName();
    }
    if (className.equals(AllApplicationsActivity.class.getName())) {
      mShowSystem = true;
    } else if (className.equals(NotificationAppListActivity.class.getName())) {
      mListType = LIST_TYPE_NOTIFICATION;
      mNotifBackend = new NotificationBackend();
    } else if (className.equals(DomainsURLsAppListActivity.class.getName())) {
      mListType = LIST_TYPE_DOMAINS_URLS;
    } else if (className.equals(StorageUseActivity.class.getName())) {
      if (args != null && args.containsKey(EXTRA_VOLUME_UUID)) {
        mVolumeUuid = args.getString(EXTRA_VOLUME_UUID);
        mVolumeName = args.getString(EXTRA_VOLUME_NAME);
        mListType = LIST_TYPE_STORAGE;
      } else {
        // No volume selected, display a normal list, sorted by size.
        mListType = LIST_TYPE_MAIN;
      }
      mSortOrder = R.id.sort_order_size;
    } else if (className.equals(UsageAccessSettingsActivity.class.getName())) {
      mListType = LIST_TYPE_USAGE_ACCESS;
      getActivity().getActionBar().setTitle(R.string.usage_access_title);
    } else if (className.equals(HighPowerApplicationsActivity.class.getName())) {
      mListType = LIST_TYPE_HIGH_POWER;
      // Default to showing system.
      mShowSystem = true;
    } else if (className.equals(OverlaySettingsActivity.class.getName())) {
      mListType = LIST_TYPE_OVERLAY;
      getActivity().getActionBar().setTitle(R.string.system_alert_window_access_title);
    } else if (className.equals(WriteSettingsActivity.class.getName())) {
      mListType = LIST_TYPE_WRITE_SETTINGS;
      getActivity().getActionBar().setTitle(R.string.write_settings_title);
    } else {
      mListType = LIST_TYPE_MAIN;
    }
    mFilter = getDefaultFilter();

    if (savedInstanceState != null) {
      mSortOrder = savedInstanceState.getInt(EXTRA_SORT_ORDER, mSortOrder);
      mShowSystem = savedInstanceState.getBoolean(EXTRA_SHOW_SYSTEM, mShowSystem);
    }

    mInvalidSizeStr = getActivity().getText(R.string.invalid_size_value);

    mResetAppsHelper = new ResetAppsHelper(getActivity());
  }
 @Override
 public void onActivityResult(int requestCode, int resultCode, Intent data) {
   if (requestCode == INSTALLED_APP_DETAILS && mCurrentPkgName != null) {
     if (mListType == LIST_TYPE_NOTIFICATION) {
       mApplications.mExtraInfoBridge.forceUpdate(mCurrentPkgName, mCurrentUid);
     } else if (mListType == LIST_TYPE_HIGH_POWER
         || mListType == LIST_TYPE_OVERLAY
         || mListType == LIST_TYPE_WRITE_SETTINGS) {
       if (mFinishAfterDialog) {
         getActivity().onBackPressed();
       } else {
         mApplications.mExtraInfoBridge.forceUpdate(mCurrentPkgName, mCurrentUid);
       }
     } else {
       mApplicationsState.requestSize(mCurrentPkgName, UserHandle.getUserId(mCurrentUid));
     }
   }
 }
 ArrayList<ApplicationsState.AppEntry> applyPrefixFilter(
     CharSequence prefix, ArrayList<ApplicationsState.AppEntry> origEntries) {
   if (prefix == null || prefix.length() == 0) {
     return origEntries;
   } else {
     String prefixStr = ApplicationsState.normalize(prefix.toString());
     final String spacePrefixStr = " " + prefixStr;
     ArrayList<ApplicationsState.AppEntry> newEntries =
         new ArrayList<ApplicationsState.AppEntry>();
     for (int i = 0; i < origEntries.size(); i++) {
       ApplicationsState.AppEntry entry = origEntries.get(i);
       String nlabel = entry.getNormalizedLabel();
       if (nlabel.startsWith(prefixStr) || nlabel.indexOf(spacePrefixStr) != -1) {
         newEntries.add(entry);
       }
     }
     return newEntries;
   }
 }
 public ApplicationsAdapter(
     ApplicationsState state, ManageApplications manageApplications, int filterMode) {
   mState = state;
   mSession = state.newSession(this);
   mManageApplications = manageApplications;
   mContext = manageApplications.getActivity();
   mPm = mContext.getPackageManager();
   mFilterMode = filterMode;
   if (mManageApplications.mListType == LIST_TYPE_NOTIFICATION) {
     mExtraInfoBridge =
         new AppStateNotificationBridge(
             mContext.getPackageManager(), mState, this, manageApplications.mNotifBackend);
   } else if (mManageApplications.mListType == LIST_TYPE_USAGE_ACCESS) {
     mExtraInfoBridge = new AppStateUsageBridge(mContext, mState, this);
   } else if (mManageApplications.mListType == LIST_TYPE_HIGH_POWER) {
     mExtraInfoBridge = new AppStatePowerBridge(mState, this);
   } else if (mManageApplications.mListType == LIST_TYPE_OVERLAY) {
     mExtraInfoBridge = new AppStateOverlayBridge(mContext, mState, this);
   } else if (mManageApplications.mListType == LIST_TYPE_WRITE_SETTINGS) {
     mExtraInfoBridge = new AppStateWriteSettingsBridge(mContext, mState, this);
   } else {
     mExtraInfoBridge = null;
   }
 }
    public void rebuild(boolean eraseold) {
      if (!mHasReceivedLoadEntries && (mExtraInfoBridge == null || mHasReceivedBridgeCallback)) {
        // Don't rebuild the list until all the app entries are loaded.
        return;
      }
      if (DEBUG) Log.i(TAG, "Rebuilding app list...");
      ApplicationsState.AppFilter filterObj;
      Comparator<AppEntry> comparatorObj;
      boolean emulated = Environment.isExternalStorageEmulated();
      if (emulated) {
        mWhichSize = SIZE_TOTAL;
      } else {
        mWhichSize = SIZE_INTERNAL;
      }
      filterObj = FILTERS[mFilterMode];
      if (mOverrideFilter != null) {
        filterObj = mOverrideFilter;
      }
      if (!mManageApplications.mShowSystem) {
        filterObj = new CompoundFilter(filterObj, ApplicationsState.FILTER_DOWNLOADED_AND_LAUNCHER);
      }
      switch (mLastSortMode) {
        case R.id.sort_order_size:
          switch (mWhichSize) {
            case SIZE_INTERNAL:
              comparatorObj = ApplicationsState.INTERNAL_SIZE_COMPARATOR;
              break;
            case SIZE_EXTERNAL:
              comparatorObj = ApplicationsState.EXTERNAL_SIZE_COMPARATOR;
              break;
            default:
              comparatorObj = ApplicationsState.SIZE_COMPARATOR;
              break;
          }
          break;
        default:
          comparatorObj = ApplicationsState.ALPHA_COMPARATOR;
          break;
      }
      ArrayList<ApplicationsState.AppEntry> entries = mSession.rebuild(filterObj, comparatorObj);
      if (entries == null && !eraseold) {
        // Don't have new list yet, but can continue using the old one.
        return;
      }
      mBaseEntries = entries;
      if (mBaseEntries != null) {
        mEntries = applyPrefixFilter(mCurFilterPrefix, mBaseEntries);
      } else {
        mEntries = null;
      }
      notifyDataSetChanged();

      if (mSession.getAllApps().size() != 0
          && mManageApplications.mListContainer.getVisibility() != View.VISIBLE) {
        Utils.handleLoadingContainer(
            mManageApplications.mLoadingContainer, mManageApplications.mListContainer, true, true);
      }
      if (mManageApplications.mListType == LIST_TYPE_USAGE_ACCESS) {
        // No enabled or disabled filters for usage access.
        return;
      }

      mManageApplications.setHasDisabled(mState.haveDisabledApps());
    }