/**
   * Method that returns the storage volumes defined in the system. This method uses reflection to
   * retrieve the method because CM10 has a {@link Context} as first parameter, that AOSP hasn't.
   *
   * @param ctx The current context
   * @return StorageVolume[] The storage volumes defined in the system
   */
  @SuppressWarnings("boxing")
  public static synchronized StorageVolume[] getStorageVolumes(Context ctx) {
    if (sStorageVolumes == null) {
      // IMP!! Android SDK doesn't have a "getVolumeList" but is supported by CM10.
      // Use reflect to get this value (if possible)
      try {
        StorageManager sm = (StorageManager) ctx.getSystemService(Context.STORAGE_SERVICE);
        Method method = sm.getClass().getMethod("getVolumeList"); // $NON-NLS-1$
        sStorageVolumes = (StorageVolume[]) method.invoke(sm);

      } catch (Exception ex) {
        // Ignore. Android SDK StorageManager class doesn't have this method
        // Use default android information from environment
        try {
          File externalStorage = Environment.getExternalStorageDirectory();
          String path = externalStorage.getCanonicalPath();
          String description = null;
          if (path.toLowerCase().indexOf("usb") != -1) { // $NON-NLS-1$
            description = ctx.getString(R.string.usb_storage);
          } else {
            description = ctx.getString(R.string.external_storage);
          }
          // Android SDK has a different constructor for StorageVolume. In CM10 the
          // description is a resource id. Create the object by reflection
          Constructor<StorageVolume> constructor =
              StorageVolume.class.getConstructor(
                  String.class,
                  String.class,
                  boolean.class,
                  boolean.class,
                  int.class,
                  boolean.class,
                  long.class);
          StorageVolume sv = constructor.newInstance(path, description, false, false, 0, false, 0);
          sStorageVolumes = new StorageVolume[] {sv};
        } catch (Exception ex2) {
          /** NON BLOCK* */
        }
      }
      if (sStorageVolumes == null) {
        sStorageVolumes = new StorageVolume[] {};
      }
    }
    return sStorageVolumes;
  }
Example #2
0
 public String[] getStorageList(Context context) {
   String[] paths = null;
   if (context != null) {
     StorageManager storageMan =
         (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
     try {
       Method methodGetPaths = storageMan.getClass().getMethod("getVolumePaths");
       try {
         paths = (String[]) methodGetPaths.invoke(storageMan);
       } catch (Exception e) {
         e.printStackTrace();
       }
     } catch (NoSuchMethodException e) {
       e.printStackTrace();
     }
   }
   return paths;
 }
Example #3
0
  /**
   * 获取多张 sd卡的 路径
   *
   * @return
   */
  public static String[] getSdCardPath() {

    StorageManager sm =
        (StorageManager) CKApplication.context.getSystemService(Context.STORAGE_SERVICE);
    String[] paths = null;
    try {
      paths = (String[]) sm.getClass().getMethod("getVolumePaths", null).invoke(sm, null);
    } catch (IllegalArgumentException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (InvocationTargetException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (NoSuchMethodException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    return paths;
  }
  private void executeInit(Context context) {
    StorageManager mStorageManager =
        (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
    if (mStorageManager != null) {
      Class<?> mStorageVolume = null;
      Method mGetVolumeListMethod = null;
      Method mGetVolumeStateMethod = null;
      Method mGetPathMethod = null;
      Method mIsRemovableMethod = null;
      Object[] mStorageVolumeList = null;
      try {
        mStorageVolume = Class.forName(CLASS_NAME);
        mGetVolumeListMethod =
            mStorageManager.getClass().getMethod(METHOD_GET_VOLUME_LIST, new Class[0]);
        mGetVolumeStateMethod =
            mStorageManager
                .getClass()
                .getMethod(METHOD_GET_VOLUME_STATE, new Class[] {String.class});
        mIsRemovableMethod = mStorageVolume.getMethod(METHOD_IS_REMOVABLE, new Class[0]);
        mGetPathMethod = mStorageVolume.getMethod(METHOD_GET_PATH, new Class[0]);

        mStorageVolumeList = (Object[]) mGetVolumeListMethod.invoke(mStorageManager, new Object[0]);

        boolean mIsRemovable = false;

        if (mStorageVolumeList != null && mStorageVolumeList.length > 0) {
          int mStorageVolumeCount = mStorageVolumeList.length;

          Log.i(TAG, "init() === > StorageVolume Count = " + mStorageVolumeCount);

          mInternalPathList.clear();
          mExternalPathList.clear();

          for (int i = 0; i < mStorageVolumeCount; ++i) {
            String mStoragePath =
                (String) mGetPathMethod.invoke(mStorageVolumeList[i], new Object[0]);
            mIsRemovable =
                ((Boolean) mIsRemovableMethod.invoke(mStorageVolumeList[i], new Object[0]))
                    .booleanValue();
            if (!TextUtils.isEmpty(mStoragePath)) {
              String state =
                  (String)
                      mGetVolumeStateMethod.invoke(mStorageManager, new Object[] {mStoragePath});
              if ((state != null) && (state.equals(MOUNTED))) {
                if (mIsRemovable) {
                  Log.i(TAG, "init() === > external storage path = (" + mStoragePath + ")");
                  mExternalPathList.add(mStoragePath);
                } else {
                  Log.i(TAG, "init() === > internal storage path = (" + mStoragePath + ")");
                  mInternalPathList.add(mStoragePath);
                }
              }
            }
          }
        }
      } catch (ClassNotFoundException e) {
        handleInvalid();
        Log.e(TAG, "init() === > Exception:ClassNotFoundException");
      } catch (NoSuchMethodException e) {
        handleInvalid();
        Log.e(TAG, "init() === > Exception:NoSuchMethodException");
      } catch (IllegalArgumentException e) {
        handleInvalid();
        Log.e(TAG, "init() === > Exception:IllegalArgumentException");
      } catch (IllegalAccessException e) {
        handleInvalid();
        Log.e(TAG, "init() === > Exception:IllegalAccessException");
      } catch (InvocationTargetException e) {
        handleInvalid();
        Log.e(TAG, "init() === > Exception:InvocationTargetException");
      }
    } else {
      handleInvalid();
      Log.e(TAG, "init() === > can't get storage manager");
    }
    initSDCardPath();
  }