Example #1
0
  private static boolean isKitKatDocument(Context context, Uri uri) {
    if (Build.VERSION.SDK_INT < 19) {
      return false;
    }

    return CompatibilityImpl.isDocumentUri(context, uri);
  }
  private View instantiate(String name, Context context, AttributeSet attrs) {
    try {
      Constructor<? extends View> constructor = CONSTRUCTOR_MAP.get(name);
      if (constructor == null) {
        Class<? extends View> clazz = null;
        if (name.indexOf('.') != -1) {
          clazz = context.getClassLoader().loadClass(name).asSubclass(View.class);
        } else {
          for (String prefix : CLASS_PREFIX_LIST) {
            try {
              clazz = context.getClassLoader().loadClass(prefix + name).asSubclass(View.class);
              break;
            } catch (ClassNotFoundException e) {
            }
          }
          if (clazz == null) throw new ClassNotFoundException("couldn't find class: " + name);
        }
        constructor = clazz.getConstructor(CONSTRUCTOR_SIGNATURE);
        CONSTRUCTOR_MAP.put(name, constructor);
      }

      Object[] args = constructorArgs;
      args[0] = context;
      args[1] = attrs;

      constructor.setAccessible(true);
      View view = constructor.newInstance(args);
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN && view instanceof ViewStub)
        CompatibilityImpl.setLayoutInflater((ViewStub) view, inflater.cloneInContext(context));
      return view;
    } catch (Exception e) {
      Logger.e(TAG, "couldn't instantiate class " + name, e);
      return null;
    }
  }
Example #3
0
  public static String getPath(final Context context, final Uri uri) {
    // DocumentProvider
    if (isKitKatDocument(context, uri)) {
      // ExternalStorageProvider
      if (isExternalStorageDocument(uri)) {
        final String docId = CompatibilityImpl.getDocumentId(uri);
        final String[] split = docId.split(":");
        final String type = split[0];

        if ("primary".equalsIgnoreCase(type)) {
          return Environment.getExternalStorageDirectory() + "/" + split[1];
        }

        // TODO handle non-primary volumes
      }
      // DownloadsProvider
      else if (isDownloadsDocument(uri)) {

        final String id = CompatibilityImpl.getDocumentId(uri);
        final Uri contentUri =
            ContentUris.withAppendedId(
                Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));

        return getDataColumn(context, contentUri, null, null);
      }
      // MediaProvider
      else if (isMediaDocument(uri)) {
        final String docId = CompatibilityImpl.getDocumentId(uri);
        final String[] split = docId.split(":");
        final String type = split[0];

        Uri contentUri = null;
        if ("image".equals(type)) {
          contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        } else if ("video".equals(type)) {
          contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
        } else if ("audio".equals(type)) {
          contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        }

        final String selection = BaseColumns._ID + "=?";
        final String[] selectionArgs = new String[] {split[1]};

        return getDataColumn(context, contentUri, selection, selectionArgs);
      }
    }
    // MediaStore (and general)
    else if ("content".equalsIgnoreCase(uri.getScheme())) {

      // Return the remote address
      if (isGooglePhotosUri(uri)) {
        return uri.getLastPathSegment();
      }

      return getDataColumn(context, uri, null, null);
    }
    // File
    else if ("file".equalsIgnoreCase(uri.getScheme())) {
      return uri.getPath();
    }

    return null;
  }