Implementation of default write disk in Android settings

Source: Internet
Author: User

native Android Settings There is no default memory options, but MTK has added this feature , may MTK think this is more useful, so, they made a change on the basis of the original, plus this feature. But Qualcomm platform does not have this function, compared to MTK, Qualcomm is more rigorous, will not casually add some features, but the MTK platform to optimize a lot of things (there are some shortcomings), developers this may be a lot of cottage phone choose MTK Platform reason it.

I made a simple definition of "default memory": Some data storage locations for built-in apps. Of course, if other applications know about this interface (not the standard interface, so only developers know, or MTK), you can also use this feature.

Before receiving a mobile phone on the Qualcomm platform with the default memory option, that is, if you choose local memory, some of the built-in application data will be stored in local memory, if you choose SD card storage, the data of these apps will be stored in the SD card. Learn from the implementation of MTK platform, the idea is: through the Systemproperties interface to the system memory to write the storage path (set), and then in some specified applications to store data in the time to determine the current default memory, using the Get method to read this property. Why use Systemproperties, because the value of its settings can be used globally, that is, different applications can take their values out of shared memory, resolving the problem of interprocess communication.

    1: We first add some custom interface files to the framework layer. The location I added is .../frameworks/base/core/java/android/os/storage/, add the filename Storagemanagerex.java, as follows:

Package Android.os.storage;import Android.util.log;import Com.android.internal.r;import Android.os.systemproperties;import Java.io.file;import Android.os.environment;public class StorageManagerEx {»priva       Te static final String TAG = "Storagemanagerex";»       private static final String Prop_sd_default_path = "Persist.sys.sd.defaultpath";»       »private static final String STORAGE_PATH_SD1 = "/storage/sdcard0";»       private static final String STORAGE_PATH_SD2 = "/storage/sdcard1";»       private static final String storage_path_sd1_ics = "/mnt/sdcard";»       private static final String storage_path_sd2_ics = "/mnt/sdcard2";»       »private static final String storage_path_share_sd = "/storage/emulated/0";»        »/**»* Return default path for writing.»        * @return Default path.»           * @hide»* @internal»*/public static String Getdefaultpath () {»string path = storage_path_sd1;» try {»»path= Systemproperties.get (Prop_sd_default_path);» »LOG.I (TAG, "Default path =" +path);»}          catch (IllegalArgumentException e) {»»log.e (TAG, "illegalargumentexception when get default path:" +e);»       }»//property'll be empty when first boot should set to Default»if (Path.equals ("")) {»       »LOG.I (TAG, "Getdefaultpath empty! set to Default");» »//here May is some problems.refer to mtk:only share SD and no swap,internal path is»»//"/stor Age/emulated/0 ", I don ' t know" share "and" no swap ", so I skip. »»//if (Featureoption.mtk_shared_sdcard &&!) Featureoption.mtk_2sdcard_swap) {»»»»//Setdefaultpath (STORAGE_PATH_SHARE_SD) ; »»//path = STORAGE_PATH_SHARE_SD; »»//} else {»»//setdefaultpath (STORAGE_PATH_SD1);   »       »       Path = STORAGE_PATH_SD1;       »»//}»»setdefaultpath (STORAGE_PATH_SD1);»          »path = storage_path_sd1;» }»»//here we don ' t care Multi user which would be a used by Google,ignore and go on. »»//mota upgrade from ICS to Jb,update DefaultPath to JB.       »if (Path.equals (storage_path_sd1_ics)) {»»path = storage_path_sd1;»          »setdefaultpath (path);»       }else if (path.equals (storage_path_sd2_ics)) {»»path = storage_path_sd2;»          »setdefaultpath (path);»          }»»LOG.I (TAG, "Getdefaultpath path=" +path);»   return path;    }/** * Set default path for APPTO storage data * is only can used by settings. * * @param path * @hide * @internal */public static void Setdefaultpath (String path) {»log.i (TAG,          "Setdefaultpath Path =" + path);»if (path = = null) {»»LOG.E (TAG, "setdefaultpath error! path = null");»          »return;»          }»»try {»»systemproperties.set (Prop_sd_default_path,path);»           } catch (IllegalArgumentException e) {»»log.e (TAG, "illegalargumentexception when Setdefaultpath:" + e);»       }}/** * Returns external SD card path.»          /SDCARD2 * @hide * @internal *///public static String Getexternalstoragepath () {»//method 1://» String sdcarddir = null; Android.os.storage.StorageManager StorageManager =//(Android.os.storage.StorageManager) Context/ /. Getsystemservice (Context.storage_service); storagevolume[] volumes = Storagemanager.getvolumelist (); for (int i = 0; i < volumes.length; i++) {//if (volumes[i].isremovable () && Volumes[i].all Owmassstorage ()) {//Sdcarddir = Volumes[I].getpath (); Break }//}//Return sdcarddir;   public static String Getexternalstoragepath () {»//method 2:string sdcarddir = null;      String state = Environment.getstoragestate (new File (STORAGE_PATH_SD2));   if (State.equals (environment.media_mounted)) {»sdcarddir = STORAGE_PATH_SD2;   }else if (state.equals (environment.media_unmounted)) {»sdcarddir = environment.media_unmounted;   }else{»sdcarddir = STORAGE_PATH_SD2;  } return Sdcarddir;    }/** * Returns internal storage path. * @return * @hide * @internal */public static String Getinternalstoragepath () {»string Sdcarddir = Nu          Ll;»          String state = Environment.getstoragestate (new File (STORAGE_PATH_SHARE_SD));»          »if (State.equals (environment.media_mounted)) {»»sdcarddir = storage_path_sd1;»        }else if (state.equals (environment.media_unmounted)) {»»  Sdcarddir = environment.media_unmounted;»          }else{»»sdcarddir = storage_path_sd1;»     }»return Sdcarddir;  }1}

The primary function of the definition is to add attribute Presist.sys.sd.default to the properties of the system memory. We can go inside the ADB to view and modify some properties 1:adb Shell 2:getprop + key (or no) Tip: We can also setprop + key + Value


2: The option to add default memory inside the settings:


Added in the file of the settings project inside the. Res/xml/device_info_memory.xml.

... <span style= "White-space:pre" ></span><preferencecategory         android:key= "Memory_select"        android:title= "@string/select_memory"/>     ...
and add radiobuttonpreference to the preferencecategory.(omitted here)

Add inside the Memroy.java file


private void Initdefaultwritedisk () {»mdefaultwritecategory = (preferencecategory) findpreference (default_write_c        Ategory_key);        list<radiobuttonpreference> defaultwritepreference = new arraylist<radiobuttonpreference> ();        storagevolume[] volumes = Mstoragemanager.getvolumelist ();        list<storagevolume> storagevolumesex = initvolumelist (volumes);        for (int i = 0; i < storagevolumesex.size (); i++) {»storagevolume volumn = Storagevolumesex.get (i);        »radiobuttonpreference Avalibiledisk = new Radiobuttonpreference (getactivity ());       »string path = Volumn.getpath ();»        Volumn path»android.util.log.i ("Haiyong.liu", "volumn.path=" +path);        »string state = Environment.getstoragestate (The new File (path));        »»avalibiledisk.setkey (path);        »avalibiledisk.settitle (Volumn.getdescription (Getactivity ())); »avalibiledisk.setpath (PATH);        »avalibiledisk.setonpreferencechangelistener (this);        »defaultwritepreference.add (Avalibiledisk);        »ANDROID.UTIL.LOG.E ("Haiyong.liu", "environment=" +state); »if (State.equals (environment.media_unmounted) | | State.equals (environment.media_removed)) {»»»}else {»»MDEFAULTWR        Itecategory.addpreference (Avalibiledisk); »»if (Path.equals (Storagemanagerex.getdefaultpath ())) {»»»avalibiledisk.setchecke        D (TRUE);        »»»mdeafultwritepathpref = Avalibiledisk;        »»}else{»»»avalibiledisk.setchecked (FALSE); »»}»}»»}} Private list<storagevolume> initvolumelist (Storagevo    Lume[] volumes) {»list<storagevolume> storagevolumes = new arraylist<storagevolume> (); »/*if (Usermanager.supportSmultipleusers ()) {»//return The current user ' s volumn list if supported,otherwise ignore.    »»storagevolume[] sv = Mstoragemanager.getvolumelistasuser (); »»for (int i = 0; i < sv.length; i++) {if (!) Not_present ". Equals (Mstoragemanager.getvolumestate (Volumes[i].getpath ()))) {Storagevolumes.add (volumes                [i]);    }}»»return Storagevolumes; »}*/»for (int i = 0; i < volumes.length; i++) {if (!) Not_present ". Equals (Mstoragemanager.getvolumestate (Volumes[i].getpath ()))) {Storagevolumes.add (volumes[i])            ;    }} return storagevolumes;  } @Override public boolean onpreferencechange (Preference Preference, Object newvalue) {if (Preference! =                Null && preference instanceof radiobuttonpreference) {if (mdeafultwritepathpref! = null) { MdeafultwrItepathpref.setchecked (FALSE);            } storagemanagerex.setdefaultpath (Preference.getkey ());            Mdeafultwritepathpref = (radiobuttonpreference) preference;        return true;    } return false; }

And then call inside the OnCreate method
Initdefaultwritedisk ()
This method, this method is placed in the Memory.java oncreate (...) method, in addition, Storagemanagerex.setdefaultpath (), this method can only be used in Settings this apk, that is, only settings this apk can be written.

3: Find the corresponding need to add the function of the project, we need to add is CAMREA2, Bluetooth, downloader, recording these several apk, find these several source store data place, change storage location to Storagemanagerex.getdefaultpath ()








Implementation of default write disk in Android settings

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.