Android Open Source - android_device Storage Assistant






From Project

Back to project page android_device.

License

The source code is released under:

[Apache License](http://www.apache.org/licenses/): Version 2.0, January 2004 =============== ## TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION ## ### 1. Definitions. ### "License" sha...

If you think the Android project android_device listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * =================================================================================================
 *                    Copyright (C) 2014 Martin Albedinsky [Wolf-ITechnologies]
 * =================================================================================================
 *         Licensed under the Apache License, Version 2.0 or later (further "License" only).
 * -------------------------------------------------------------------------------------------------
 * You may use this file only in compliance with the License. More details and copy of this License 
 * you may obtain at//www.  ja v a 2  s.co  m
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * You can redistribute, modify or publish any part of the code written within this file but as it 
 * is described in the License, the software distributed under the License is distributed on an 
 * "AS IS" BASIS, WITHOUT WARRANTIES or CONDITIONS OF ANY KIND.
 * 
 * See the License for the specific language governing permissions and limitations under the License.
 * =================================================================================================
 */
package com.wit.android.device.examples.module;

import android.content.Context;
import android.os.AsyncTask;
import android.text.TextUtils;
import android.util.Log;

import com.wit.android.device.AndroidDevice;
import com.wit.android.device.Storage;
import com.wit.android.device.examples.R;

import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.util.List;

/**
 * todo: description
 *
 * @author Martin Albedinsky
 */
public class StorageAssistant {

  /**
   * Log TAG.
   */
  private static final String TAG = StorageAssistant.class.getSimpleName();

  /**
   *
   */
  public static final int ACTION_DISCARD = R.id.ex_menu_action_discard;

  /**
   *
   */
  public static final int ACTION_COPY = R.id.ex_menu_action_copy;

  /**
   *
   */
  public static final int ACTION_MOVE = R.id.ex_menu_action_move;

  /**
   *
   */
  final Storage mStorage;

  /**
   *
   */
  private FilesActionTask mFilesActionTask;

  /**
   *
   */
  private StorageWatcher mStorageWatcher;

  /**
   *
   */
  private String[] mClipboardPaths;

  /**
   *
   */
  private int mClipboardAction;

  /**
   *
   * @param context
   */
  public StorageAssistant(Context context) {
    this.mStorage = AndroidDevice.getInstance(context).getStorage();
  }

  /**
   *
   * @param watcher
   */
  public void setStorageWatcher(StorageWatcher watcher) {
    this.mStorageWatcher = watcher;
  }

  /**
   *
   * @param path
   * @param fileFilter
   * @param filenameFilter
   * @param callback
   */
  public void loadFiles(String path, FileFilter fileFilter, FilenameFilter filenameFilter, LoaderCallback callback) {
    if (!TextUtils.isEmpty(path)) {
      new FilesLoaderTask(callback, fileFilter, filenameFilter).execute(path);
    }
  }

  /**
   *
   * @param paths
   */
  public void discardFiles(String... paths) {
    this.executeFilesActionTask(ACTION_DISCARD, "", paths);
  }

  /**
   *
   * @param destinationPath
   */
  public void performClipboardAction(String destinationPath) {
    if (isInClipboardMode()) {
      switch (mClipboardAction) {
        case ACTION_COPY:
          this.executeFilesActionTask(ACTION_COPY, destinationPath, mClipboardPaths);
          break;
        case ACTION_MOVE:
          this.executeFilesActionTask(ACTION_MOVE, destinationPath, mClipboardPaths);
          break;
      }
      clearClipboard();
    }
  }

  /**
   *
   * @param action
   * @param paths
   */
  public void updateClipboard(int action, String... paths) {
    this.mClipboardAction = action;
    this.mClipboardPaths = paths;
  }

  /**
   *
   */
  public void clearClipboard() {
    this.mClipboardPaths = null;
    this.mClipboardAction = 0;
  }

  /**
   *
   * @return
   */
  public boolean isInClipboardMode() {
    return mClipboardPaths != null && mClipboardPaths.length > 0;
  }

  /**
   *
   * @param action
   * @param result
   */
  void onFilesActionFinished(int action, Storage.BaseResult result) {
    this.mFilesActionTask = null;
    clearClipboard();
    if (mStorageWatcher != null) {
      mStorageWatcher.onStorageResult(action, result);
    }
  }

  /**
   *
   * @param action
   * @param basePath
   * @param paths
   */
  private void executeFilesActionTask(int action, String basePath, String... paths) {
    if (paths.length == 0) {
      return;
    }

    if (mFilesActionTask == null) {
      (mFilesActionTask = new FilesActionTask(basePath, paths)).execute(action);
    } else {
      Log.e(TAG, "FilesActionTask is already running. Can not to start another one.");
    }
  }

  /**
   *
   */
  public static interface StorageWatcher {

    /**
     *
     * @param action
     * @param result
     */
    public void onStorageResult(int action, Storage.BaseResult result);
  }

  /**
   *
   */
  public static interface LoaderCallback {

    /**
     *
     * @param files
     */
    public void onFilesLoaded(List<File> files);
  }

  /**
   *
   */
  private class FilesLoaderTask extends AsyncTask<String, Void, List<File>> {

    /**
     *
     */
    final LoaderCallback callback;

    /**
     *
     */
    final FileFilter filter;

    /**
     *
     */
    final FilenameFilter filenameFilter;

    /**
     *
     * @param callback
     */
    FilesLoaderTask(LoaderCallback callback, FileFilter filter, FilenameFilter filenameFilter) {
      this.callback = callback;
      this.filter = filter;
      this.filenameFilter = filenameFilter;
    }

    /**
     */
    @Override
    protected List<File> doInBackground(String... params) {
      return mStorage.getDirectoryContent(Storage.BASE, filter, filenameFilter, params[0]);
    }

    /**
     */
    @Override
    protected void onPostExecute(List<File> files) {
      if (callback != null) {
        callback.onFilesLoaded(files);
      }
    }
  }

  /**
   *
   */
  private class FilesActionTask extends AsyncTask<Integer, Void, Storage.BaseResult> {

    /**
     *
     */
    final String[] paths;

    /**
     *
     */
    final String destinationPath;

    /**
     *
     */
    int action;

    /**
     * @param destinationPath
     * @param paths
     */
    FilesActionTask(String destinationPath, String... paths) {
      this.destinationPath = destinationPath;
      this.paths = paths;
    }

    /**
     */
    @Override
    protected Storage.BaseResult doInBackground(Integer... params) {
      this.action = params[0];
      switch (action) {
        case ACTION_DISCARD:
          Log.d(TAG, "Deleting paths(" + paths.length + ").");
          return mStorage.deleteFilesOrDirectories(paths);
        case ACTION_COPY:
          Log.d(TAG, "Copying paths(" + paths.length + ").");
          return mStorage.copyFilesOrDirectories(Storage.DEFAULT, destinationPath, paths);
        case ACTION_MOVE:
          Log.d(TAG, "Moving paths(" + paths.length + ").");
          return mStorage.moveFilesOrDirectories(Storage.DEFAULT, destinationPath, paths);
      }
      return null;
    }

    /**
     */
    @Override
    protected void onPostExecute(Storage.BaseResult result) {
      if (result != null) {
        onFilesActionFinished(action, result);
      }
    }
  }
}




Java Source Code List

com.wit.android.device.AndroidDevice.java
com.wit.android.device.BatteryImpl.java
com.wit.android.device.Battery.java
com.wit.android.device.ConnectionImpl.java
com.wit.android.device.Connection.java
com.wit.android.device.DeviceConfig.java
com.wit.android.device.ScreenImpl.java
com.wit.android.device.Screen.java
com.wit.android.device.StorageAction.java
com.wit.android.device.StorageImpl.java
com.wit.android.device.Storage.java
com.wit.android.device.examples.HomeActivity.java
com.wit.android.device.examples.adapter.BatteryInfoAdapter.java
com.wit.android.device.examples.adapter.ConnectionInfoAdapter.java
com.wit.android.device.examples.adapter.FilesAdapter.java
com.wit.android.device.examples.adapter.OrientationsAdapter.java
com.wit.android.device.examples.adapter.SimpleInfoAdapter.java
com.wit.android.device.examples.adapter.StorageAdapter.java
com.wit.android.device.examples.dialog.NewFileDialog.java
com.wit.android.device.examples.fragment.BaseDeviceFragment.java
com.wit.android.device.examples.fragment.BatteryInfoFragment.java
com.wit.android.device.examples.fragment.ConnectionInfoFragment.java
com.wit.android.device.examples.fragment.DeviceInfoFragment.java
com.wit.android.device.examples.fragment.FragmentsFactory.java
com.wit.android.device.examples.fragment.ScreenInfoFragment.java
com.wit.android.device.examples.fragment.ScreenInterfaceFragment.java
com.wit.android.device.examples.fragment.StorageFilesFragment.java
com.wit.android.device.examples.fragment.StorageInfoFragment.java
com.wit.android.device.examples.fragment.StorageInterfaceFragment.java
com.wit.android.device.examples.model.BatteryInfo.java
com.wit.android.device.examples.model.ConnectionInfo.java
com.wit.android.device.examples.model.SimpleInfo.java
com.wit.android.device.examples.model.StorageItem.java
com.wit.android.device.examples.module.StorageAssistant.java
com.wit.android.device.receiver.BatteryHealthReceiver.java
com.wit.android.device.receiver.BatteryPluggedStateReceiver.java
com.wit.android.device.receiver.BatteryStatusReceiver.java
com.wit.android.device.receiver.BroadcastProcessor.java
com.wit.android.device.receiver.ConnectionStateReceiver.java
com.wit.android.device.util.ConnectionUtils.java
com.wit.android.device.util.ScreenUtils.java
com.wit.android.device.util.StorageEditor.java
com.wit.android.device.util.StorageUtils.java