@SuppressLint("NewApi")
 private void startDownload() {
   downloadId = PreferencesUtils.getLong(mContext, DOWNLOAD_KEY, DEFAULT_DOWNLOAD_ID);
   if (downloadId == DEFAULT_DOWNLOAD_ID) {
     DownloadManager.Request request = new DownloadManager.Request(Uri.parse(apkUrl));
     // 获取apk名称
     String apkName =
         mContext.getApplicationInfo().loadLabel(mContext.getPackageManager()).toString();
     // 设置下载地址
     request.setDestinationInExternalPublicDir(
         Constants.UPDATE_FILE_DIR, Constants.UPDATE_FILE_NAME);
     request.setTitle(apkName);
     request.setDescription("正在下载...");
     // 提示一直显示,下载完成后点击则消失
     request.setNotificationVisibility(
         DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
     request.setVisibleInDownloadsUi(false);
     // 加入下载队列
     downloadId = mDownloadManager.enqueue(request);
     PreferencesUtils.putLong(mContext, DOWNLOAD_KEY, downloadId);
   }
 }
  public static void persistentBehavior(Activity self, Integer fieldID, String prefID) {
    SharedPreferences preferences = PreferencesUtils.getPreferences(self);
    String uiValue = UIUtils.getTextById(self, fieldID);

    String value = preferences.getString(prefID, "");
    if (value.equals("")) PreferencesUtils.write(preferences, prefID, uiValue);
    else UIUtils.setText(self, fieldID, value);
  }
 /**
  * Stops the recording.
  *
  * @param context the context
  * @param trackRecordingServiceConnection the track recording service connection
  * @param showEditor true to show the editor
  */
 public static void stopRecording(
     Context context,
     TrackRecordingServiceConnection trackRecordingServiceConnection,
     boolean showEditor) {
   ITrackRecordingService trackRecordingService =
       trackRecordingServiceConnection.getServiceIfBound();
   if (trackRecordingService != null) {
     try {
       if (showEditor) {
         /*
          * Need to remember the recordingTrackId before calling
          * endCurrentTrack. endCurrentTrack sets the value to -1L.
          */
         long recordingTrackId =
             PreferencesUtils.getLong(context, R.string.recording_track_id_key);
         trackRecordingService.endCurrentTrack();
         if (recordingTrackId != PreferencesUtils.RECORDING_TRACK_ID_DEFAULT) {
           Intent intent =
               IntentUtils.newIntent(context, TrackEditActivity.class)
                   .putExtra(TrackEditActivity.EXTRA_TRACK_ID, recordingTrackId)
                   .putExtra(TrackEditActivity.EXTRA_NEW_TRACK, true);
           context.startActivity(intent);
         }
       } else {
         trackRecordingService.endCurrentTrack();
       }
     } catch (Exception e) {
       Log.e(TAG, "Unable to stop recording.", e);
     }
   } else {
     resetRecordingState(context);
   }
   trackRecordingServiceConnection.unbindAndStop();
 }
 private static void resetRecordingState(Context context) {
   long recordingTrackId = PreferencesUtils.getLong(context, R.string.recording_track_id_key);
   if (recordingTrackId != PreferencesUtils.RECORDING_TRACK_ID_DEFAULT) {
     PreferencesUtils.setLong(
         context, R.string.recording_track_id_key, PreferencesUtils.RECORDING_TRACK_ID_DEFAULT);
   }
   boolean recordingTrackPaused =
       PreferencesUtils.getBoolean(
           context,
           R.string.recording_track_paused_key,
           PreferencesUtils.RECORDING_TRACK_PAUSED_DEFAULT);
   if (!recordingTrackPaused) {
     PreferencesUtils.setBoolean(
         context,
         R.string.recording_track_paused_key,
         PreferencesUtils.RECORDING_TRACK_PAUSED_DEFAULT);
   }
 }
 @Override
 public void onReceive(Context context, Intent intent) {
   long completeDownloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
   if (completeDownloadId == downloadId) {
     // if download successful, install apk
     if (getStatusById(downloadId) == DownloadManager.STATUS_SUCCESSFUL) {
       PreferencesUtils.putLong(mContext, DOWNLOAD_KEY, DEFAULT_DOWNLOAD_ID);
       String apkFilePath =
           new StringBuilder(Environment.getExternalStorageDirectory().getAbsolutePath())
               .append(File.separator)
               .append("")
               .append(File.separator)
               .append(mContext.getApplicationInfo().loadLabel(mContext.getPackageManager()))
               .toString();
       install(context, apkFilePath);
     }
   }
 }
Beispiel #6
0
  /**
   * Gets the track name.
   *
   * @param context the context
   * @param trackId the track id
   * @param startTime the track start time
   * @param location the track location
   */
  public static String getTrackName(
      Context context, long trackId, long startTime, Location location) {
    String trackName =
        PreferencesUtils.getString(
            context, R.string.track_name_key, PreferencesUtils.TRACK_NAME_DEFAULT);

    if (trackName.equals(
        context.getString(R.string.settings_recording_track_name_date_local_value))) {
      if (startTime == -1L) {
        return null;
      }
      return StringUtils.formatDateTime(context, startTime);
    } else if (trackName.equals(
        context.getString(R.string.settings_recording_track_name_date_iso_8601_value))) {
      if (startTime == -1L) {
        return null;
      }
      SimpleDateFormat dateFormat = new SimpleDateFormat(ISO_8601_FORMAT);
      return dateFormat.format(startTime);
    } else if (trackName.equals(
        context.getString(R.string.settings_recording_track_name_number_value))) {
      if (trackId == -1L) {
        return null;
      }
      return context.getString(R.string.track_name_format, trackId);
    } else {
      // trackName equals settings_recording_track_name_location_value or any
      // value
      if (location != null) {
        return getReverseGeoCoding(context, location);
      } else {
        // Use the startTime if available
        if (startTime != -1L) {
          return StringUtils.formatDateTime(context, startTime);
        }
        return null;
      }
    }
  }