protected void initDialogView() {
    LogUtils.d(TAG, "[initDialogView]");
    mDialogView =
        LayoutInflater.from(mContext)
            .inflate(R.layout.mtk_international_country_select_list, null, false);

    ListView countryListView = (ListView) mDialogView.findViewById(R.id.list);
    mListAdapter =
        new SimpleAdapter(
            mContext,
            mCountryInfoMapList,
            R.layout.mtk_international_country_select_list_item,
            new String[] {MAP_KEY_NAME_COUNTRY_NAME, MAP_KEY_NAME_COUNTRY_CODE},
            new int[] {R.id.country_name, R.id.country_code});
    countryListView.setAdapter(mListAdapter);
    countryListView.setOnItemClickListener(this);

    mSearchView = (SearchView) mDialogView.findViewById(R.id.search);
    mSearchView.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
    mSearchView.setIconifiedByDefault(true);
    mSearchView.setQueryHint(mContext.getString(com.android.internal.R.string.search_go));
    mSearchView.setIconified(false);
    mSearchView.setOnQueryTextListener(this);

    mNoSearchResult = (TextView) mDialogView.findViewById(R.id.no_search_result);
    mCountryListView = (ListView) mDialogView.findViewById(R.id.list);

    mMoreButton = (Button) mDialogView.findViewById(R.id.more);
    mMoreButton.setText(mContext.getString(com.android.internal.R.string.more_item_label));
    mMoreButton.setOnClickListener(this);
  }
 @Override
 protected void onDestroy() {
   super.onDestroy();
   LogUtils.d(LOG_TAG, "[onDestroy]");
   if (mVCardCacheThread != null && mVCardCacheThread.isAlive()) {
     Log.w(LOG_TAG, "[onDestroy]should not finish Activity when work did not finished");
     mVCardCacheThread.cancel();
   }
 }
 /** Restore the list state after the adapter is populated. */
 protected void completeRestoreInstanceState() {
   if (mListState != null) {
     mListView.onRestoreInstanceState(mListState);
     LogUtils.d(
         TAG,
         "completeRestoreInstanceState(),the Activity may be killed..Restore the listview state last");
     mListState = null;
   }
 }
 @Override
 public void onServiceConnected(ComponentName name, IBinder binder) {
   mService = ((VCardService.MyBinder) binder).getService();
   /// M: Error handling, Activity killed might cause mVCardCacheThread un-inited. @{
   if (mVCardCacheThread == null) {
     LogUtils.e(LOG_TAG, "[onServiceConnected]mVCardCacheThread is null, some error happens.");
     VcardUtils.showErrorInfo(
         R.string.vcard_import_request_rejected_message, ImportVCardActivity.this);
     return;
   }
   /// @}
   Log.i(
       LOG_TAG,
       String.format(
           "Connected to VCardService. Kick a vCard cache thread (uri: %s)",
           Arrays.toString(mVCardCacheThread.getSourceUris())));
   mVCardCacheThread.start();
 }
  /**
   * Scans vCard in external storage (typically SDCard) and tries to import it. - When there's no
   * SDCard available, an error dialog is shown. - When multiple vCard files are available, asks a
   * user to select one.
   */
  private void doScanExternalStorageAndImportVCard() {
    // TODO: should use getExternalStorageState().
    /// M: New Feature ALPS00276020,
    //  support multiple storages for import/export. @{
    String path = VcardUtils.getPath(mSourcePath);
    final File file =
        VcardUtils.getDirectory(path, Environment.getExternalStorageDirectory().toString());
    LogUtils.d("[doScanExternalStorageAndImportVCard]", "file.path : " + file.getPath());
    /// @}
    /** M: Bug Fix, CR ID: ALPS00301464 */
    mVolumeName = VcardUtils.getVolumeName(path, this);

    if (!file.exists() || !file.isDirectory() || !file.canRead()) {
      showDialog(R.id.dialog_sdcard_not_found);
    } else {
      mVCardScanThread = new VCardScanThread(file);
      showDialog(R.id.dialog_searching_vcard);
    }
  }
 public AccountWithDataSetEx(String name, String type, String dataSet) {
   super(name, type, dataSet);
   mSubId = SubscriptionManager.INVALID_SUB_ID;
   LogUtils.i(TAG, "[AccountWithDataSetEx]name:" + name + "type:" + type + " subId:" + mSubId);
 }
 public String getDisplayName() {
   String displayName = SubInfoUtils.getDisplaynameUsingSubId(mSubId);
   LogUtils.i(TAG, "[getDisplayName]displayName:" + displayName + ", subId:" + mSubId);
   return displayName;
 }
 public long getSubId() {
   LogUtils.i(TAG, "[getSubId]subId:" + mSubId);
   return mSubId;
 }
 /**
  * in some case,restore the ListView last user will replace the new state. so should provider this
  * function to child class to prevent restore state.
  */
 protected void clearListViewLastState() {
   LogUtils.d(TAG, "#clearListViewLastState()");
   mListState = null;
 }
    @Override
    public void run() {
      Log.i(LOG_TAG, "vCard cache thread starts running.");
      if (mConnection == null) {
        throw new NullPointerException(
            "vCard cache thread must be launched " + "after a service connection is established");
      }

      mWakeLock.acquire();
      try {
        /// M: ALPS01022668 first vcard import finished,
        //  second vcard still caching, can't stop service in this case.
        mConnection.setVCardCaching(true);
        if (mCanceled == true) {
          Log.i(LOG_TAG, "vCard cache operation is canceled.");
          return;
        }

        final Context context = ImportVCardActivity.this;
        // Uris given from caller applications may not be opened twice: consider when
        // it is not from local storage (e.g. "file:///...") but from some special
        // provider (e.g. "content://...").
        // Thus we have to once copy the content of Uri into local storage, and read
        // it after it.
        //
        // We may be able to read content of each vCard file during copying them
        // to local storage, but currently vCard code does not allow us to do so.
        int cache_index = 0;
        ArrayList<ImportRequest> requests = new ArrayList<ImportRequest>();
        if (mSource != null) {
          try {
            requests.add(constructImportRequest(mSource, null, mDisplayName));
          } catch (VCardException e) {
            Log.e(LOG_TAG, "Maybe the file is in wrong format", e);
            showFailureNotification(R.string.fail_reason_not_supported);
            return;
          }
        } else {
          final ContentResolver resolver = ImportVCardActivity.this.getContentResolver();
          for (Uri sourceUri : mSourceUris) {
            String filename = null;
            // Note: caches are removed by VCardService.
            while (true) {
              filename = VCardService.CACHE_FILE_PREFIX + cache_index + ".vcf";
              final File file = context.getFileStreamPath(filename);
              if (!file.exists()) {
                break;
              } else {
                if (cache_index == Integer.MAX_VALUE) {
                  throw new RuntimeException("Exceeded cache limit");
                }
                cache_index++;
              }
            }
            final Uri localDataUri = copyTo(sourceUri, filename);
            if (mCanceled) {
              Log.i(LOG_TAG, "vCard cache operation is canceled.");
              break;
            }
            if (localDataUri == null) {
              Log.w(LOG_TAG, "destUri is null");
              break;
            }

            String displayName = null;
            Cursor cursor = null;
            // Try to get a display name from the given Uri. If it fails, we just
            // pick up the last part of the Uri.
            try {
              cursor =
                  resolver.query(
                      sourceUri, new String[] {OpenableColumns.DISPLAY_NAME}, null, null, null);
              if (cursor != null && cursor.getCount() > 0 && cursor.moveToFirst()) {
                if (cursor.getCount() > 1) {
                  Log.w(LOG_TAG, "Unexpected multiple rows: " + cursor.getCount());
                }
                int index = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
                if (index >= 0) {
                  displayName = cursor.getString(index);
                }
              }
              /// M: @{
            } catch (Exception e) {
              // we just want the name. so exception should be igrnored. anyway, if
              // displayName is not set, the follwing code will use given uri as display name.
              Log.w(LOG_TAG, "exception caught ", e);
              /// @}
            } finally {
              if (cursor != null) {
                cursor.close();
              }
            }
            if (TextUtils.isEmpty(displayName)) {
              displayName = sourceUri.getLastPathSegment();
            }

            final ImportRequest request;
            try {
              request = constructImportRequest(null, localDataUri, displayName);
            } catch (VCardException e) {
              Log.e(LOG_TAG, "Maybe the file is in wrong format", e);
              /// M: Fixed cr ALPS00598462 @{
              String reason = getString(R.string.fail_reason_not_supported);
              VcardUtils.showFailureNotification(
                  ImportVCardActivity.this, reason, displayName, cache_index, mHandler);
              continue;
              /// @}
              /// Bug fix ALPS00318987 @{
            } catch (IllegalArgumentException e) {
              LogUtils.e(LOG_TAG, "Maybe the file is in wrong format", e);
              /// M: Fixed cr ALPS00598462
              String reason = getString(R.string.fail_reason_not_supported);
              VcardUtils.showFailureNotification(
                  ImportVCardActivity.this, reason, displayName, cache_index, mHandler);
              continue;
              /// @}
            } catch (IOException e) {
              Log.e(LOG_TAG, "Unexpected IOException", e);
              /// M: Fixed cr ALPS00598462 @{
              String reason = getString(R.string.fail_reason_io_error);
              VcardUtils.showFailureNotification(
                  ImportVCardActivity.this, reason, displayName, cache_index, mHandler);
              continue;
              /// @}
            }
            if (mCanceled) {
              Log.i(LOG_TAG, "vCard cache operation is canceled.");
              return;
            }
            requests.add(request);
          }
        }
        if (!requests.isEmpty()) {
          mConnection.sendImportRequest(requests);
        } else {
          Log.w(LOG_TAG, "Empty import requests. Ignore it.");
        }
      } catch (OutOfMemoryError e) {
        Log.e(LOG_TAG, "OutOfMemoryError occured during caching vCard");
        System.gc();
        runOnUiThread(
            new DialogDisplayer(getString(R.string.fail_reason_low_memory_during_import)));
      } catch (FileNotFoundException e) {
        /// M: Fix ALPS00874343,if the vcf file is not exist,toast error msg. @{
        LogUtils.w(LOG_TAG, "[run] the vcf file is not found when import! exception:" + e);
        VcardUtils.showErrorInfo(R.string.import_failure_no_vcard_file, ImportVCardActivity.this);
        /// @}
      } catch (IOException e) {
        Log.e(LOG_TAG, "IOException during caching vCard", e);
        runOnUiThread(new DialogDisplayer(getString(R.string.fail_reason_io_error)));
      } finally {
        Log.i(LOG_TAG, "Finished caching vCard.");
        /// M: ALPS01022668 first vcard import finished,
        //  second vcard still caching, can't stop service in this case.
        mConnection.setVCardCaching(false);
        mWakeLock.release();
        /// M: @{
        if (!ImportVCardActivity.this.isFinishing()
            && (null != mConnection)
            && mConnection.isServiceBinded()) {
          unbindService(mConnection);
          mConnection = null;
        } else {
          LogUtils.d(LOG_TAG, "in VcardCacheThread, Run(), mConnection==null !!! ");
        }
        /// @}
        mProgressDialogForCachingVCard.dismiss();
        mProgressDialogForCachingVCard = null;
        finish();
      }
    }
 /**
  * M: ALPS01022668 first vcard import finished, second vcard still caching, can't stop service
  * in this case. @{
  */
 public void setVCardCaching(boolean cache) {
   LogUtils.d(LOG_TAG, "setVCardCaching: " + cache);
   mService.mCaching = cache;
 }
  @Override
  protected Dialog onCreateDialog(int resId, Bundle bundle) {
    switch (resId) {
      case R.string.import_from_sdcard:
        {
          LogUtils.d(LOG_TAG, "[onCreateDialog]import_from_sdcard");
          if (mAccountSelectionListener == null) {
            throw new NullPointerException("mAccountSelectionListener must not be null.");
          }
          return AccountSelectionUtil.getSelectAccountDialog(
              this, resId, mAccountSelectionListener, mCancelListener);
        }
      case R.id.dialog_searching_vcard:
        {
          LogUtils.d(LOG_TAG, "[onCreateDialog]dialog_search_vcard");
          /// M: Change for CR ALPS00683398. This will be called from
          //  Activity.restoreManagedDialogs() when the contacts process has been killed;
          //  So add checking "mVCardScanThread != null" before use it.
          if (mProgressDialogForScanVCard == null && mVCardScanThread != null) {
            String message = getString(R.string.searching_vcard_message);
            mProgressDialogForScanVCard = ProgressDialog.show(this, "", message, true, false);
            mProgressDialogForScanVCard.setOnCancelListener(mVCardScanThread);
            mVCardScanThread.start();
          }
          return mProgressDialogForScanVCard;
        }
      case R.id.dialog_sdcard_not_found:
        {
          LogUtils.d(LOG_TAG, "[onCreateDialog]dialog_sdcard_not_found");
          AlertDialog.Builder builder =
              new AlertDialog.Builder(this)
                  .setIconAttribute(android.R.attr.alertDialogIcon)
                  .setMessage(R.string.no_sdcard_message)
                  .setOnCancelListener(mCancelListener)
                  .setPositiveButton(android.R.string.ok, mCancelListener);
          return builder.create();
        }
      case R.id.dialog_vcard_not_found:
        {
          LogUtils.d(LOG_TAG, "[onCreateDialog]dialog_vcard_not_found");
          /** M: Bug Fix CR ID: ALPS00301464 @} */
          final String message = getString(R.string.import_no_vcard_dialog_text, mVolumeName);
          AlertDialog.Builder builder =
              new AlertDialog.Builder(this)
                  .setMessage(message)
                  .setOnCancelListener(mCancelListener)
                  .setPositiveButton(android.R.string.ok, mCancelListener);
          /** @} */
          return builder.create();
        }
      case R.id.dialog_select_import_type:
        {
          LogUtils.d(LOG_TAG, "[onCreateDialog]dialog_select_import_type");
          return getSelectImportTypeDialog();
        }
      case R.id.dialog_select_multiple_vcard:
        {
          /** M: @{ */
          LogUtils.d(LOG_TAG, "[onCreateDialog]dialog_select_multiple_vcard");
          mVcardFileSelectDialog = (AlertDialog) getVCardFileSelectDialog(true);
          return mVcardFileSelectDialog;
          /** @} */
        }
      case R.id.dialog_select_one_vcard:
        {
          LogUtils.d(LOG_TAG, "[onCreateDialog]dialog_select_one_vcard");
          return getVCardFileSelectDialog(false);
        }
      case R.id.dialog_cache_vcard:
        {
          LogUtils.d(LOG_TAG, "[onCreateDialog]dialog_cache_vcard");
          if (mProgressDialogForCachingVCard == null) {
            LogUtils.d(LOG_TAG, "[onCreateDialog][dialog_cache_vcard]Dialog first created");
            final String title = getString(R.string.caching_vcard_title);
            final String message = getString(R.string.caching_vcard_message);
            mProgressDialogForCachingVCard = new ProgressDialog(this);
            mProgressDialogForCachingVCard.setTitle(title);
            mProgressDialogForCachingVCard.setMessage(message);
            mProgressDialogForCachingVCard.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            mProgressDialogForCachingVCard.setOnCancelListener(mVCardCacheThread);
            startVCardService();
          }
          return mProgressDialogForCachingVCard;
        }
      case R.id.dialog_io_exception:
        {
          LogUtils.d(LOG_TAG, "[onCreateDialog]dialog_io_exception");
          String message =
              (getString(
                  R.string.scanning_sdcard_failed_message,
                  getString(R.string.fail_reason_io_error)));
          AlertDialog.Builder builder =
              new AlertDialog.Builder(this)
                  .setIconAttribute(android.R.attr.alertDialogIcon)
                  .setMessage(message)
                  .setOnCancelListener(mCancelListener)
                  .setPositiveButton(android.R.string.ok, mCancelListener);
          return builder.create();
        }
      case R.id.dialog_error_with_message:
        {
          LogUtils.d(LOG_TAG, "[onCreateDialog]dialog_error_with_message");
          String message = mErrorMessage;
          if (TextUtils.isEmpty(message)) {
            Log.e(LOG_TAG, "Error message is null while it must not.");
            message = getString(R.string.fail_reason_unknown);
          }
          final AlertDialog.Builder builder =
              new AlertDialog.Builder(this)
                  .setTitle(getString(R.string.reading_vcard_failed_title))
                  .setIconAttribute(android.R.attr.alertDialogIcon)
                  .setMessage(message)
                  .setOnCancelListener(mCancelListener)
                  .setPositiveButton(android.R.string.ok, mCancelListener);
          return builder.create();
        }
      default:
        {
          LogUtils.w(LOG_TAG, "[onCreateDialog]res id is invalid: " + resId);
        }
    }

    return super.onCreateDialog(resId, bundle);
  }