Android Open Source - ColorWallpaper Storage Utility






From Project

Back to project page ColorWallpaper.

License

The source code is released under:

Licensed under the Expat License. Copyright (C) 2013 Peter Occil Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the...

If you think the Android project ColorWallpaper 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

package com.upokecenter.android.util;
/*w  w  w .  ja  v a  2s .  co m*/
import java.io.File;
import java.util.Locale;

import android.Manifest;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Environment;

import com.upokecenter.util.DateTimeUtility;
import com.upokecenter.util.Reflection;

public final class StorageUtility {
  private StorageUtility(){}

  public static File getPrivateCachePath(Context ctx){
    return getPrivateCachePath(ctx,null);
  }

  public static File getCachePath(Context ctx){
    return getCachePath(ctx,null);
  }


  public static File getPrivateCachePath(Context ctx, String name){
    File cacheroot=ctx.getCacheDir();
    if(cacheroot==null)
      return (name==null || name.length()==0) ? null : new File(name);
    else
      return (name==null || name.length()==0) ? cacheroot : new File(cacheroot,name);
  }



  public static File getCachePath(Context ctx, String name){
    File cacheroot=null;
    // Check if we can write to external storage
    if(ctx.checkCallingOrSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)==
        PackageManager.PERMISSION_GRANTED){
      // getExternalCacheDir added in API level 8 (Froyo)
      cacheroot=(File)Reflection.invokeByName(ctx,"getExternalCacheDir",null);
      if(cacheroot==null){
        cacheroot=new File(Environment.getExternalStorageDirectory(),"Android");
        cacheroot=new File(cacheroot,"data");
        cacheroot=new File(cacheroot,ctx.getApplicationInfo().packageName);
        cacheroot=new File(cacheroot,"cache");
      }
      // isExternalStorageRemovable added in API level 9 (Gingerbread)
      String mounted=Environment.MEDIA_MOUNTED;
      boolean removable=(Boolean)Reflection.invokeStaticByName(Environment.class,
          "isExternalStorageRemovable",true);
      if(removable && (!mounted.equals(Environment.getExternalStorageState()))){
        cacheroot=null;
      }
      if(cacheroot!=null)
        return (name==null || name.length()==0) ? cacheroot : new File(cacheroot,name);
    }
    return getPrivateCachePath(ctx,name);
  }

  public static File getCameraFolderUniqueFileName(){
    if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
      File storagedir=new File(Environment.getExternalStorageDirectory(),"DCIM");
      // For the camera app to save images, the directory must
      // exist: it won't make the directory for us.
      // So check if it exists
      File subdir=new File(storagedir,"Camera");
      if(subdir.isDirectory())
        return getUniqueFileName(subdir);
      if(storagedir.isDirectory())
        return getUniqueFileName(storagedir);
      storagedir=new File(Environment.getExternalStorageDirectory(),"Pictures");
      if(storagedir.isDirectory())
        return getUniqueFileName(storagedir);
      // Use the external storage directory itself as a last resort
      storagedir=Environment.getExternalStorageDirectory();
      if(storagedir.isDirectory())
        return getUniqueFileName(storagedir);
    }
    return null;
  }

  public static File getUniqueFileName(File storage){
    if(storage==null)return null;
    int[] components=DateTimeUtility.getCurrentLocalDateComponents();
    int i=0;
    while(true){
      String string=String.format(Locale.US,
          "%04d-%02d-%02d-%d.png",
          components[0],
          components[1],
          components[2],i
          );
      File file=new File(storage,string);
      if(!file.exists())
        return file;
      i++;
    }
  }

  public static long getDefaultCacheSize(Context context){
    ActivityManager mgr=((ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE));
    long memory=Runtime.getRuntime().maxMemory();
    long m2=(Integer)Reflection.invokeByName(mgr,"getMemoryClass",-1);
    if(m2>=0){
      memory=Math.min(memory,m2*1024L*1024L);
    }
    long defaultCacheSize=Math.max(1L*1024L*1024L,memory/4);
    return defaultCacheSize;
  }
}




Java Source Code List

com.upokecenter.android.colorwallpaper.ColorWallpaperService.java
com.upokecenter.android.colorwallpaper.LauncherActivity.java
com.upokecenter.android.colorwallpaper.SettingsActivity.java
com.upokecenter.android.location.DummyLocationHelper.java
com.upokecenter.android.location.ILocationHelper.java
com.upokecenter.android.location.ISimpleLocationListener.java
com.upokecenter.android.location.LocationHelper.java
com.upokecenter.android.net.ConnectivityHelper.java
com.upokecenter.android.net.DownloadService.java
com.upokecenter.android.net.IConnectionListener.java
com.upokecenter.android.ui.AlertDialogActivity.java
com.upokecenter.android.ui.AlertDialogPreference.java
com.upokecenter.android.ui.BaseSettingsActivity.java
com.upokecenter.android.ui.ChoosePicturePreference.java
com.upokecenter.android.ui.ColorPickerDialog.java
com.upokecenter.android.ui.ContinuousValuePreference.java
com.upokecenter.android.ui.DialogUtility.java
com.upokecenter.android.ui.GetContentActivity.java
com.upokecenter.android.ui.IChoiceListener.java
com.upokecenter.android.ui.IDialogUpdater.java
com.upokecenter.android.ui.IntentPreference.java
com.upokecenter.android.ui.PreferenceState.java
com.upokecenter.android.ui.ShareActivity.java
com.upokecenter.android.ui.UriPreference.java
com.upokecenter.android.util.AppManager.java
com.upokecenter.android.util.BitmapUtility.java
com.upokecenter.android.util.StorageUtility.java
com.upokecenter.android.wallpaper.BaseWallpaperService.java
com.upokecenter.util.XmlHelper.java