author | Ting Li <ting.li@amlogic.com> | 2016-10-31 06:15:56 (GMT) |
---|---|---|
committer | Ting Li <ting.li@amlogic.com> | 2016-10-31 06:28:28 (GMT) |
commit | 13c5f8962a0882dc76a5bdc12baa081a0373e559 (patch) | |
tree | 97af4bebefb38b7eaee492492a7316e4710d80d7 | |
parent | ef5e3505d664e1f1c4910024a3b181b88421b447 (diff) | |
download | OTAUpgrade2-13c5f8962a0882dc76a5bdc12baa081a0373e559.zip OTAUpgrade2-13c5f8962a0882dc76a5bdc12baa081a0373e559.tar.gz OTAUpgrade2-13c5f8962a0882dc76a5bdc12baa081a0373e559.tar.bz2 |
pd#133884 scan user 0 directory update file
Change-Id: I80559fb257701e1a977824a851032d520fcb562b
-rw-r--r-- | src/com/droidlogic/otaupgrade/FileSelector.java | 442 | ||||
-rw-r--r-- | src/com/droidlogic/otaupgrade/PrefUtils.java | 989 |
2 files changed, 730 insertions, 701 deletions
diff --git a/src/com/droidlogic/otaupgrade/FileSelector.java b/src/com/droidlogic/otaupgrade/FileSelector.java index 086f720..b43e5f8 100644 --- a/src/com/droidlogic/otaupgrade/FileSelector.java +++ b/src/com/droidlogic/otaupgrade/FileSelector.java @@ -1,221 +1,221 @@ -/**
- * @Package com.amlogic.otauicase
- * @Description Copyright (c) Inspur Group Co., Ltd. Unpublished Inspur Group
- * Co., Ltd. Proprietary & Confidential This source code and the
- * algorithms implemented therein constitute confidential
- * information and may comprise trade secrets of Inspur or its
- * associates, and any use thereof is subject to the terms and
- * conditions of the Non-Disclosure Agreement pursuant to which
- * this source code was originally received.
- */
-package com.droidlogic.otaupgrade;
-
-import android.app.Activity;
-import android.app.ProgressDialog;
-
-import android.content.Context;
-import android.content.Intent;
-
-import android.os.Build;
-import android.os.Bundle;
-import android.os.Handler;
-import android.os.Message;
-
-import android.util.AttributeSet;
-import android.util.Log;
-
-import android.view.KeyEvent;
-import android.view.LayoutInflater;
-import android.view.View;
-import android.view.ViewGroup;
-
-import android.widget.AdapterView;
-
-import android.widget.AdapterView.OnItemClickListener;
-
-import android.widget.BaseAdapter;
-import android.widget.ListView;
-import android.widget.TextView;
-
-import java.io.File;
-import java.io.FilenameFilter;
-
-import java.util.ArrayList;
-
-
-/**
- * @ClassName FileSelector
- * @Description TODO
- * @Date 2013-7-16
- * @Email
- * @Author
- * @Version V1.0
- */
-public class FileSelector extends Activity implements OnItemClickListener {
- private static final String TAG = "FileSelector";
- public static final String FILE = "file";
- private static final int MSG_HIDE_SHOW_DIALOG = 1;
- private static final int MSG_SHOW_WAIT_DIALOG = 2;
- private static final int MSG_NOTIFY_DATACHANGE = 3;
- private static final int WAITDIALOG_DISPALY_TIME = 500;
- private PrefUtils mPrefUtil;
- private File mCurrentDirectory;
- private LayoutInflater mInflater;
- private FileAdapter mAdapter = new FileAdapter();
- private ListView mListView;
- private ProgressDialog mPdWatingScan = null;
- private Handler mHandler = new Handler() {
- @Override
- public void handleMessage ( Message msg ) {
- super.handleMessage ( msg );
- switch ( msg.what ) {
- case MSG_SHOW_WAIT_DIALOG:
- mPdWatingScan = ProgressDialog.show ( FileSelector.this,
- getResources().getString ( R.string.scan_title ),
- getResources().getString ( R.string.scan_tip ) );
- break;
- case MSG_HIDE_SHOW_DIALOG:
- removeMessages ( MSG_SHOW_WAIT_DIALOG );
- if ( mPdWatingScan != null ) {
- mPdWatingScan.dismiss();
- mPdWatingScan = null;
- }
- break;
- case MSG_NOTIFY_DATACHANGE:
- mAdapter.notifyDataSetChanged();
- break;
- default:
- break;
- }
- }
- };
-
- private void startScanThread() {
- Message nmsg = mHandler.obtainMessage ( MSG_SHOW_WAIT_DIALOG );
- mHandler.sendMessageDelayed ( nmsg, WAITDIALOG_DISPALY_TIME );
- new Thread() {
- public void run() {
- ArrayList<File> files = mPrefUtil.getExternalStorageList();
- mAdapter.getList ( files );
- mHandler.sendEmptyMessage ( MSG_HIDE_SHOW_DIALOG );
- }
- } .start();
- }
-
- @Override
- protected void onCreate ( Bundle savedInstanceState ) {
- super.onCreate ( savedInstanceState );
- mInflater = LayoutInflater.from ( this );
- setContentView ( R.layout.file_list );
- mListView = ( ListView ) findViewById ( R.id.file_list );
- mListView.setAdapter ( mAdapter );
-
- mListView.setOnItemClickListener ( this );
- mPrefUtil = new PrefUtils(this);
- startScanThread();
- }
-
- @Override
- public void onItemClick ( AdapterView<?> adapterView, View view,
- int position, long id ) {
- File selectFile = ( File ) adapterView.getItemAtPosition ( position );
- if ( selectFile.isFile() ) {
- Intent intent = new Intent();
- intent.putExtra ( FILE, selectFile.getPath() );
- setResult ( 0, intent );
- finish();
- }
- }
-
-
- private class FileAdapter extends BaseAdapter {
- private File[] mFiles;
- private ArrayList<File> files = new ArrayList();
-
- public void setCurrentList ( File directory ) {
- File[] tempFiles = directory.listFiles ( new ZipFileFilter() );
- for ( int i = 0; ( tempFiles != null ) && ( i < tempFiles.length );
- i++ ) {
- if ( tempFiles[i].isDirectory() ) {
- setCurrentList ( tempFiles[i] );
- } else {
- files.add ( tempFiles[i] );
- }
- }
- }
-
- public void getList ( ArrayList<File> dir ) {
- if ( dir == null) {
- return;
- }
- for (int i = 0; i < dir.size(); i++ ) {
- File directory = dir.get(i);
- setCurrentList ( directory );
- }
- mFiles = new File[files.size()];
- for ( int i = 0; i < files.size(); i++ ) {
- mFiles[i] = ( File ) files.get ( i );
- }
- mHandler.sendEmptyMessage ( MSG_NOTIFY_DATACHANGE );
- }
-
- public void getList ( File[] dir ) {
- for ( int j = 0; j < dir.length; j++ ) {
- setCurrentList ( dir[j] );
- }
- mFiles = new File[files.size()];
- for ( int i = 0; i < files.size(); i++ ) {
- mFiles[i] = ( File ) files.get ( i );
- }
- mHandler.sendEmptyMessage ( MSG_NOTIFY_DATACHANGE );
- }
-
- @Override
- public int getCount() {
- return ( mFiles == null ) ? 0 : mFiles.length;
- }
-
- @Override
- public File getItem ( int position ) {
- File file = ( mFiles == null ) ? null : mFiles[position];
- return file;
- }
-
- @Override
- public long getItemId ( int position ) {
- return position;
- }
-
- @Override
- public View getView ( int position, View convertView, ViewGroup parent ) {
- if ( convertView == null ) {
- convertView = mInflater.inflate ( R.layout.large_text, null );
- }
- TextView tv = ( TextView ) convertView;
- File file = mFiles[position];
- String name = file.getPath();
- String nameDest = null;
- if (mPrefUtil != null) {
- nameDest = mPrefUtil.getTransPath(name);
- }
- else {
- nameDest = name;
- }
- tv.setText ( nameDest );
- return tv;
- }
- }
-
- class ZipFileFilter implements FilenameFilter {
- public boolean accept ( File directory, String file ) {
- String dir = directory.getPath();
- if ( new File ( directory, file ).isDirectory() ) {
- return true;
- } else if ( file.toLowerCase().endsWith ( ".zip" )) {
- return true;
- } else {
- return false;
- }
- }
- }
-}
+/** + * @Package com.amlogic.otauicase + * @Description Copyright (c) Inspur Group Co., Ltd. Unpublished Inspur Group + * Co., Ltd. Proprietary & Confidential This source code and the + * algorithms implemented therein constitute confidential + * information and may comprise trade secrets of Inspur or its + * associates, and any use thereof is subject to the terms and + * conditions of the Non-Disclosure Agreement pursuant to which + * this source code was originally received. + */ +package com.droidlogic.otaupgrade; + +import android.app.Activity; +import android.app.ProgressDialog; + +import android.content.Context; +import android.content.Intent; + +import android.os.Build; +import android.os.Bundle; +import android.os.Handler; +import android.os.Message; + +import android.util.AttributeSet; +import android.util.Log; + +import android.view.KeyEvent; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import android.widget.AdapterView; + +import android.widget.AdapterView.OnItemClickListener; + +import android.widget.BaseAdapter; +import android.widget.ListView; +import android.widget.TextView; + +import java.io.File; +import java.io.FilenameFilter; + +import java.util.ArrayList; + + +/** + * @ClassName FileSelector + * @Description TODO + * @Date 2013-7-16 + * @Email + * @Author + * @Version V1.0 + */ +public class FileSelector extends Activity implements OnItemClickListener { + private static final String TAG = "FileSelector"; + public static final String FILE = "file"; + private static final int MSG_HIDE_SHOW_DIALOG = 1; + private static final int MSG_SHOW_WAIT_DIALOG = 2; + private static final int MSG_NOTIFY_DATACHANGE = 3; + private static final int WAITDIALOG_DISPALY_TIME = 500; + private PrefUtils mPrefUtil; + private File mCurrentDirectory; + private LayoutInflater mInflater; + private FileAdapter mAdapter = new FileAdapter(); + private ListView mListView; + private ProgressDialog mPdWatingScan = null; + private Handler mHandler = new Handler() { + @Override + public void handleMessage ( Message msg ) { + super.handleMessage ( msg ); + switch ( msg.what ) { + case MSG_SHOW_WAIT_DIALOG: + mPdWatingScan = ProgressDialog.show ( FileSelector.this, + getResources().getString ( R.string.scan_title ), + getResources().getString ( R.string.scan_tip ) ); + break; + case MSG_HIDE_SHOW_DIALOG: + removeMessages ( MSG_SHOW_WAIT_DIALOG ); + if ( mPdWatingScan != null ) { + mPdWatingScan.dismiss(); + mPdWatingScan = null; + } + break; + case MSG_NOTIFY_DATACHANGE: + mAdapter.notifyDataSetChanged(); + break; + default: + break; + } + } + }; + + private void startScanThread() { + Message nmsg = mHandler.obtainMessage ( MSG_SHOW_WAIT_DIALOG ); + mHandler.sendMessageDelayed ( nmsg, WAITDIALOG_DISPALY_TIME ); + new Thread() { + public void run() { + ArrayList<File> files = mPrefUtil.getMainStorageList(); + mAdapter.getList ( files ); + mHandler.sendEmptyMessage ( MSG_HIDE_SHOW_DIALOG ); + } + } .start(); + } + + @Override + protected void onCreate ( Bundle savedInstanceState ) { + super.onCreate ( savedInstanceState ); + mInflater = LayoutInflater.from ( this ); + setContentView ( R.layout.file_list ); + mListView = ( ListView ) findViewById ( R.id.file_list ); + mListView.setAdapter ( mAdapter ); + + mListView.setOnItemClickListener ( this ); + mPrefUtil = new PrefUtils(this); + startScanThread(); + } + + @Override + public void onItemClick ( AdapterView<?> adapterView, View view, + int position, long id ) { + File selectFile = ( File ) adapterView.getItemAtPosition ( position ); + if ( selectFile.isFile() ) { + Intent intent = new Intent(); + intent.putExtra ( FILE, selectFile.getPath() ); + setResult ( 0, intent ); + finish(); + } + } + + + private class FileAdapter extends BaseAdapter { + private File[] mFiles; + private ArrayList<File> files = new ArrayList(); + + public void setCurrentList ( File directory ) { + File[] tempFiles = directory.listFiles ( new ZipFileFilter() ); + for ( int i = 0; ( tempFiles != null ) && ( i < tempFiles.length ); + i++ ) { + if ( tempFiles[i].isDirectory() ) { + setCurrentList ( tempFiles[i] ); + } else { + files.add ( tempFiles[i] ); + } + } + } + + public void getList ( ArrayList<File> dir ) { + if ( dir == null) { + return; + } + for (int i = 0; i < dir.size(); i++ ) { + File directory = dir.get(i); + setCurrentList ( directory ); + } + mFiles = new File[files.size()]; + for ( int i = 0; i < files.size(); i++ ) { + mFiles[i] = ( File ) files.get ( i ); + } + mHandler.sendEmptyMessage ( MSG_NOTIFY_DATACHANGE ); + } + + public void getList ( File[] dir ) { + for ( int j = 0; j < dir.length; j++ ) { + setCurrentList ( dir[j] ); + } + mFiles = new File[files.size()]; + for ( int i = 0; i < files.size(); i++ ) { + mFiles[i] = ( File ) files.get ( i ); + } + mHandler.sendEmptyMessage ( MSG_NOTIFY_DATACHANGE ); + } + + @Override + public int getCount() { + return ( mFiles == null ) ? 0 : mFiles.length; + } + + @Override + public File getItem ( int position ) { + File file = ( mFiles == null ) ? null : mFiles[position]; + return file; + } + + @Override + public long getItemId ( int position ) { + return position; + } + + @Override + public View getView ( int position, View convertView, ViewGroup parent ) { + if ( convertView == null ) { + convertView = mInflater.inflate ( R.layout.large_text, null ); + } + TextView tv = ( TextView ) convertView; + File file = mFiles[position]; + String name = file.getPath(); + String nameDest = null; + if (mPrefUtil != null) { + nameDest = mPrefUtil.getTransPath(name); + } + else { + nameDest = name; + } + tv.setText ( nameDest ); + return tv; + } + } + + class ZipFileFilter implements FilenameFilter { + public boolean accept ( File directory, String file ) { + String dir = directory.getPath(); + if ( new File ( directory, file ).isDirectory() ) { + return true; + } else if ( file.toLowerCase().endsWith ( ".zip" )) { + return true; + } else { + return false; + } + } + } +} diff --git a/src/com/droidlogic/otaupgrade/PrefUtils.java b/src/com/droidlogic/otaupgrade/PrefUtils.java index d4fd1ed..c30a5f7 100644 --- a/src/com/droidlogic/otaupgrade/PrefUtils.java +++ b/src/com/droidlogic/otaupgrade/PrefUtils.java @@ -1,480 +1,509 @@ -/**
- * @Package com.amlogic.otauicase
- * @Description Copyright (c) Inspur Group Co., Ltd. Unpublished Inspur Group
- * Co., Ltd. Proprietary & Confidential This source code and the
- * algorithms implemented therein constitute confidential
- * information and may comprise trade secrets of Inspur or its
- * associates, and any use thereof is subject to the terms and
- * conditions of the Non-Disclosure Agreement pursuant to which
- * this source code was originally received.
- */
-package com.droidlogic.otaupgrade;
-
-import android.content.Context;
-import android.content.SharedPreferences;
-
-import android.os.Environment;
-import android.util.Log;
-import java.io.BufferedWriter;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.FileWriter;
-import java.io.IOException;
-import com.amlogic.update.DownloadUpdateTask;
-
-import android.os.storage.StorageManager;
-import java.util.HashSet;
-import java.util.Locale;
-import java.util.List;
-import java.util.Set;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.BufferedInputStream;
-import java.io.BufferedOutputStream;
-import java.nio.channels.FileChannel;
-import com.amlogic.update.OtaUpgradeUtils;
-import java.lang.reflect.Method;
-import java.lang.reflect.Field;
-/**
- * @ClassName PrefUtils
- * @Description TODO
- * @Date 2013-7-16
- * @Email
- * @Author
- * @Version V1.0
- */
-public class PrefUtils implements DownloadUpdateTask.CheckPathCallBack{
- public static Boolean DEBUG = false;
- public static final String TAG = "OTA";
- public static final String EXTERNAL_STORAGE = "/external_storage/";
- private static final String PREFS_DOWNLOAD_FILELIST = "download_filelist";
- private static final String PREFS_UPDATE_FILEPATH = "update_file_path";
- private static final String PREFS_UPDATE_SCRIPT = "update_with_script";
- private static final String PREFS_UPDATE_FILESIZE = "update_file_size";
- private static final String PREFS_UPDATE_DESC = "update_desc";
- public static final String DEV_PATH = "/storage/external_storage";
- public static final String PREF_START_RESTORE = "retore_start";
- public static final String PREF_AUTO_CHECK = "auto_check";
- static final String FlagFile = ".wipe_record";
- private Context mContext;
- private SharedPreferences mPrefs;
-
- PrefUtils ( Context context ) {
- mPrefs = context.getSharedPreferences ( "update", Context.MODE_PRIVATE );
- mContext = context;
- }
-
- private void setString ( String key, String Str ) {
- SharedPreferences.Editor mEditor = mPrefs.edit();
- mEditor.putString ( key, Str );
- mEditor.commit();
- }
-
- private void setStringSet ( String key, Set<String> downSet ) {
- SharedPreferences.Editor mEditor = mPrefs.edit();
- mEditor.putStringSet ( key, downSet );
- mEditor.commit();
- }
-
- private void setInt ( String key, int Int ) {
- SharedPreferences.Editor mEditor = mPrefs.edit();
- mEditor.putInt ( key, Int );
- mEditor.commit();
- }
-
- public void setDescrib ( String desc ) {
- setString ( PREFS_UPDATE_DESC, desc );
- }
-
- public String getDescri() {
- return mPrefs.getString ( PREFS_UPDATE_DESC, "" );
- }
-
- private void setLong ( String key, long Long ) {
- SharedPreferences.Editor mEditor = mPrefs.edit();
- mEditor.putLong ( key, Long );
- mEditor.commit();
- }
-
- void setBoolean ( String key, Boolean bool ) {
- SharedPreferences.Editor mEditor = mPrefs.edit();
- mEditor.putBoolean ( key, bool );
- mEditor.commit();
- }
-
- public void setScriptAsk ( boolean bool ) {
- setBoolean ( PREFS_UPDATE_SCRIPT, bool );
- }
-
- public boolean getScriptAsk() {
- return mPrefs.getBoolean ( PREFS_UPDATE_SCRIPT, false );
- }
-
- void setDownFileList ( Set<String> downlist ) {
- if ( downlist.size() > 0 ) {
- setStringSet ( PREFS_DOWNLOAD_FILELIST, downlist );
- }
- }
-
- Set<String> getDownFileSet() {
- return mPrefs.getStringSet ( PREFS_DOWNLOAD_FILELIST, null );
- }
-
- boolean getBooleanVal ( String key, boolean def ) {
- return mPrefs.getBoolean ( key, def );
- }
-
- public void setUpdatePath ( String path ) {
- setString ( PREFS_UPDATE_FILEPATH, path );
- }
-
- public String getUpdatePath() {
- String path = mPrefs.getString ( PREFS_UPDATE_FILEPATH, null );
- path = onExternalPathSwitch(path);
- return path;
- }
-
- public long getFileSize() {
- return mPrefs.getLong ( PREFS_UPDATE_FILESIZE, 0 );
- }
-
- public void saveFileSize ( long fileSize ) {
- setLong ( PREFS_UPDATE_FILESIZE, fileSize );
- }
-
- public static Object getProperties(String key, String def) {
- String defVal = def;
- try {
- Class properClass = Class.forName("android.os.SystemProperties");
- Method getMethod = properClass.getMethod("get",String.class,String.class);
- defVal = (String)getMethod.invoke(null,key,def);
- } catch (Exception ex) {
- ex.printStackTrace();
- } finally {
- Log.d(TAG,"getProperty:"+key+" defVal:"+defVal);
- return defVal;
- }
-
- }
- static boolean isUserVer() {
- String userVer = (String)getProperties("ro.secure",null);//SystemProperties.get ( "ro.secure", "" );
- String userDebug = (String)getProperties("ro.debuggable","0");//SystemProperties.get ( "ro.debuggable", "" );
- String hideLocalUp = (String)getProperties("ro.otaupdate.local",null);//SystemProperties.get ( "ro.otaupdate.local", "" );
- if ( ( hideLocalUp != null ) && hideLocalUp.equals ( "1" ) ) {
- if ( ( userVer != null ) && ( userVer.length() > 0 ) ) {
- return ( userVer.trim().equals ( "1" ) ) && ( userDebug.equals ( "0" ) );
- }
- }
- return false;
- }
-
- public static boolean getAutoCheck() {
- String auto = (String)getProperties("ro.product.update.autocheck","false");
- return ( "true" ).equals ( auto );
- }
-
- public ArrayList<File> getExternalStorageList(){
- Class<?> volumeInfoC = null;
- Method getvolume = null;
- Method isMount = null;
- Method getType = null;
- Method getPath = null;
- List<?> mVolumes = null;
- StorageManager mStorageManager = (StorageManager)mContext.getSystemService(Context.STORAGE_SERVICE);
- ArrayList<File> devList = new ArrayList<File>();
- try {
- volumeInfoC = Class.forName("android.os.storage.VolumeInfo");
- getvolume = StorageManager.class.getMethod("getVolumes");
- isMount = volumeInfoC.getMethod("isMountedReadable");
- getType = volumeInfoC.getMethod("getType");
- getPath = volumeInfoC.getMethod("getPath");
- mVolumes = (List<?>)getvolume.invoke(mStorageManager);
-
- for (Object vol : mVolumes) {
- if (vol != null && (boolean)isMount.invoke(vol) && (int)getType.invoke(vol) == 0) {
- devList.add((File)getPath.invoke(vol));
- Log.d(TAG, "path.getName():" + getPath.invoke(vol));
- }
- }
- }catch (Exception ex) {
- ex.printStackTrace();
- }finally {
- return devList;
- }
- }
-
- public Object getDiskInfo(String filePath){
- StorageManager mStorageManager = (StorageManager)mContext.getSystemService(Context.STORAGE_SERVICE);
- Class<?> volumeInfoC = null;
- Class<?> deskInfoC = null;
- Method getvolume = null;
- Method getDisk = null;
- Method isMount = null;
- Method getPath = null;
- Method getType = null;
- List<?> mVolumes = null;
- try {
- volumeInfoC = Class.forName("android.os.storage.VolumeInfo");
- deskInfoC = Class.forName("android.os.storage.DiskInfo");
- getvolume = StorageManager.class.getMethod("getVolumes");
- mVolumes = (List<?>)getvolume.invoke(mStorageManager);//mStorageManager.getVolumes();
- isMount = volumeInfoC.getMethod("isMountedReadable");
- getDisk = volumeInfoC.getMethod("getDisk");
- getPath = volumeInfoC.getMethod("getPath");
- getType = volumeInfoC.getMethod("getType");
- for (Object vol : mVolumes) {
- if (vol != null && (boolean)isMount.invoke(vol) && ((int)getType.invoke(vol) == 0)) {
- Object info = getDisk.invoke(vol);
- Log.d(TAG, "getDiskInfo" +((File)getPath.invoke(vol)).getAbsolutePath());
- if ( info != null && filePath.contains(((File)getPath.invoke(vol)).getAbsolutePath()) ) {
- Log.d(TAG, "getDiskInfo path.getName():" +((File)getPath.invoke(vol)).getAbsolutePath());
- return info;
- }
- }
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- return null;
-
- }
-
- public String getTransPath(String inPath) {
- String outPath = inPath;
- String pathLast;
- String pathVol;
- int idx = -1;
- int len;
- Class<?> volumeInfoC = null;
- Method getBestVolumeDescription = null;
- Method getVolumes = null;
- Method getType = null;
- Method isMount = null;
- Method getPath = null;
- List<?> volumes = null;
- StorageManager storageManager = (StorageManager)mContext.getSystemService(Context.STORAGE_SERVICE);
- try {
- volumeInfoC = Class.forName("android.os.storage.VolumeInfo");
- getVolumes = StorageManager.class.getMethod("getVolumes",StorageManager.class);
- volumes = (List)getVolumes.invoke(storageManager);
- isMount = volumeInfoC.getMethod("isMountedReadable");
- getType = volumeInfoC.getMethod("getType");
- getPath = volumeInfoC.getMethod("getPath");
- for (Object vol : volumes) {
- if (vol != null && (boolean)isMount.invoke(vol) && (int)getType.invoke(vol) == 0) {
- pathVol = ((File)getPath.invoke(vol)).getAbsolutePath();
- idx = inPath.indexOf(pathVol);
- if (idx != -1) {
- len = pathVol.length();
- pathLast = inPath.substring(idx + len);
- getBestVolumeDescription = StorageManager.class.getMethod("getBestVolumeDescription",volumeInfoC);
-
- outPath = ((String)getBestVolumeDescription.invoke(storageManager,vol)) + pathLast;
- }
- }
- }
- } catch (Exception ex) {
- } finally {
- return outPath;
- }
-
- }
- private String getCanWritePath(){
- ArrayList<File> externalDevs = getExternalStorageList();
- String filePath = "";
- for ( int j = 0; (externalDevs != null) && j < externalDevs.size(); j++ ) {
- File dir = externalDevs.get(j);
- if ( dir.isDirectory() && dir.canWrite() ) {
- filePath = dir.getAbsolutePath();
- filePath += "/";
- break;
- }
- }
- return filePath;
- }
-
- public int createAmlScript(String fullpath, boolean wipe_data, boolean wipe_cache) {
-
- File file;
- String res = "";
- int UpdateMode = OtaUpgradeUtils.UPDATE_UPDATE;
- file = new File("/cache/recovery/command");
-
- try {
- File parent = file.getParentFile();
- if (file.exists()) {
- file.delete();
- }
- if (!parent.exists()) {
- parent.mkdirs();
- }
-
- if (!file.exists()) {
- file.createNewFile();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- res += "--update_package=";
- Class<?> deskInfoClass = null;
- Method isSd = null;
- Method isUsb = null;
- Object info = getDiskInfo(fullpath);
- if (info == null) {
- res += "/cache/";
- UpdateMode = OtaUpgradeUtils.UPDATE_OTA;
- } else {
- try {
- deskInfoClass = Class.forName("android.os.storage.DiskInfo");
- isSd = deskInfoClass.getMethod("isSd");
- isUsb = deskInfoClass.getMethod("isUsb");
- if ( info != null ) {
- if ( (boolean)isSd.invoke(info) ) {
- res += "/sdcard/";
- }else if ( (boolean)isUsb.invoke(info) ) {
- res += "/udisk/";
- }else {
- res += "/cache/";
- UpdateMode = OtaUpgradeUtils.UPDATE_OTA;
- }
- } else {
- res += "/cache/";
- UpdateMode = OtaUpgradeUtils.UPDATE_OTA;
- }
-
- } catch (Exception ex) {
- ex.printStackTrace();
- res += "/cache/";
- UpdateMode = OtaUpgradeUtils.UPDATE_OTA;
- }
- }
- res += new File(fullpath).getName();
- res += ("\n--locale=" + Locale.getDefault().toString());
- res += (wipe_data? "\n--wipe_data" : "");
- res += (wipe_cache? "\n--wipe_media" : "");
-
- //res += (mWipeCache.isChecked() ? "\n--wipe_cache" : "");
- try {
- FileOutputStream fout = new FileOutputStream(file);
- byte[] buffer = res.getBytes();
- fout.write(buffer);
- fout.close();
- } catch (IOException e) {
- e.printStackTrace();
- Log.d(TAG, "IOException:" + this.getClass());
- }
- return UpdateMode;
- }
-
- void write2File() {
- String flagParentPath = getCanWritePath();
- if ( flagParentPath.isEmpty() ) {
- return;
- }
- File flagFile = new File ( flagParentPath, FlagFile );
- if ( !flagFile.exists() ) {
- try {
- flagFile.createNewFile();
- } catch ( IOException excep ) {
- }
- }
- if ( !flagFile.canWrite() ) {
- return;
- }
-
- FileWriter fw = null;
- try {
- fw = new FileWriter ( flagFile );
- } catch ( IOException excep ) {
- }
- BufferedWriter output = new BufferedWriter ( fw );
- Set<String> downfiles = mPrefs.getStringSet ( PREFS_DOWNLOAD_FILELIST,
- null );
- if ( ( downfiles != null ) && ( downfiles.size() > 0 ) ) {
- String[] downlist = downfiles.toArray ( new String[0] );
- for ( int i = 0; i < downlist.length; i++ ) {
- try {
- output.write ( downlist[i] );
- output.newLine();
- } catch ( IOException ex ) {
- }
- }
- }
- try {
- output.close();
- } catch ( IOException e ) {
- }
- }
-
- public void copyBackup(boolean outside){
- String backupInrFile = "/data/data/com.droidlogic.otaupgrade/BACKUP";
- String backupOutFile = getCanWritePath();
-
- - File dev = new File ( backupOutFile );
- if ( backupOutFile.isEmpty() || dev == null || !dev.canWrite() ) {
- return;
- }
- if ( dev.isDirectory() && dev.canWrite() && !dev.getName().startsWith(".") ) {
- backupOutFile = dev.getAbsolutePath();
- backupOutFile += "/BACKUP";
- if ( !backupOutFile.equals ( "" ) ) {
- try {
- if ( outside )
- copyFile ( backupInrFile, backupOutFile );
- else
- copyFile ( backupOutFile, backupInrFile);
- } catch ( Exception ex ) {
- ex.printStackTrace();
- }
- }
- }
- }
-
- public void copyBKFile() {
- copyBackup(true);
- }
-
- -
- public String onExternalPathSwitch(String filePath) {
- if ( filePath.contains(EXTERNAL_STORAGE) || filePath.contains(EXTERNAL_STORAGE.toUpperCase()) ) {
- String path = getCanWritePath();
- if ( path != null && !path.isEmpty() ) {
- filePath = filePath.replace(EXTERNAL_STORAGE,path);
- }
- }
- return filePath;
- }
-
- - public static void copyFile ( String fileFromPath, String fileToPath ) throws Exception {
-
- FileInputStream fi = null;
- FileOutputStream fo = null;
- FileChannel in = null;
- FileChannel out = null;
- Log.d(TAG,"copyFile from "+fileFromPath+" to "+fileToPath);
- try {
- fi = new FileInputStream ( new File ( fileFromPath ) );
- in = fi.getChannel();
- fo = new FileOutputStream ( new File ( fileToPath ) );
- out = fo.getChannel();
- in.transferTo ( 0, in.size(), out );
- } finally {
- try{
- fi.close();
- fo.close();
- in.close();
- out.close();
- }catch(Exception ex){
- }
- }
- }
-
-}
+/** + * @Package com.amlogic.otauicase + * @Description Copyright (c) Inspur Group Co., Ltd. Unpublished Inspur Group + * Co., Ltd. Proprietary & Confidential This source code and the + * algorithms implemented therein constitute confidential + * information and may comprise trade secrets of Inspur or its + * associates, and any use thereof is subject to the terms and + * conditions of the Non-Disclosure Agreement pursuant to which + * this source code was originally received. + */ +package com.droidlogic.otaupgrade; + +import android.content.Context; +import android.content.SharedPreferences; + +import android.os.Environment; +import android.util.Log; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.FileWriter; +import java.io.IOException; +import com.amlogic.update.DownloadUpdateTask; + +import android.os.storage.StorageManager; +import java.util.HashSet; +import java.util.Locale; +import java.util.List; +import java.util.Set; +import java.util.ArrayList; +import java.util.Collections; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.nio.channels.FileChannel; +import com.amlogic.update.OtaUpgradeUtils; +import java.lang.reflect.Method; +import java.lang.reflect.Field; +/** + * @ClassName PrefUtils + * @Description TODO + * @Date 2013-7-16 + * @Email + * @Author + * @Version V1.0 + */ +public class PrefUtils implements DownloadUpdateTask.CheckPathCallBack{ + public static Boolean DEBUG = false; + public static final String TAG = "OTA"; + public static final String EXTERNAL_STORAGE = "/external_storage/"; + private static final String PREFS_DOWNLOAD_FILELIST = "download_filelist"; + private static final String PREFS_UPDATE_FILEPATH = "update_file_path"; + private static final String PREFS_UPDATE_SCRIPT = "update_with_script"; + private static final String PREFS_UPDATE_FILESIZE = "update_file_size"; + private static final String PREFS_UPDATE_DESC = "update_desc"; + public static final String DEV_PATH = "/storage/external_storage"; + public static final String PREF_START_RESTORE = "retore_start"; + public static final String PREF_AUTO_CHECK = "auto_check"; + static final String FlagFile = ".wipe_record"; + private Context mContext; + private SharedPreferences mPrefs; + + PrefUtils ( Context context ) { + mPrefs = context.getSharedPreferences ( "update", Context.MODE_PRIVATE ); + mContext = context; + } + + private void setString ( String key, String Str ) { + SharedPreferences.Editor mEditor = mPrefs.edit(); + mEditor.putString ( key, Str ); + mEditor.commit(); + } + + private void setStringSet ( String key, Set<String> downSet ) { + SharedPreferences.Editor mEditor = mPrefs.edit(); + mEditor.putStringSet ( key, downSet ); + mEditor.commit(); + } + + private void setInt ( String key, int Int ) { + SharedPreferences.Editor mEditor = mPrefs.edit(); + mEditor.putInt ( key, Int ); + mEditor.commit(); + } + + public void setDescrib ( String desc ) { + setString ( PREFS_UPDATE_DESC, desc ); + } + + public String getDescri() { + return mPrefs.getString ( PREFS_UPDATE_DESC, "" ); + } + + private void setLong ( String key, long Long ) { + SharedPreferences.Editor mEditor = mPrefs.edit(); + mEditor.putLong ( key, Long ); + mEditor.commit(); + } + + void setBoolean ( String key, Boolean bool ) { + SharedPreferences.Editor mEditor = mPrefs.edit(); + mEditor.putBoolean ( key, bool ); + mEditor.commit(); + } + + public void setScriptAsk ( boolean bool ) { + setBoolean ( PREFS_UPDATE_SCRIPT, bool ); + } + + public boolean getScriptAsk() { + return mPrefs.getBoolean ( PREFS_UPDATE_SCRIPT, false ); + } + + void setDownFileList ( Set<String> downlist ) { + if ( downlist.size() > 0 ) { + setStringSet ( PREFS_DOWNLOAD_FILELIST, downlist ); + } + } + + Set<String> getDownFileSet() { + return mPrefs.getStringSet ( PREFS_DOWNLOAD_FILELIST, null ); + } + + boolean getBooleanVal ( String key, boolean def ) { + return mPrefs.getBoolean ( key, def ); + } + + public void setUpdatePath ( String path ) { + setString ( PREFS_UPDATE_FILEPATH, path ); + } + + public String getUpdatePath() { + String path = mPrefs.getString ( PREFS_UPDATE_FILEPATH, null ); + path = onExternalPathSwitch(path); + return path; + } + + public long getFileSize() { + return mPrefs.getLong ( PREFS_UPDATE_FILESIZE, 0 ); + } + + public void saveFileSize ( long fileSize ) { + setLong ( PREFS_UPDATE_FILESIZE, fileSize ); + } + + public static Object getProperties(String key, String def) { + String defVal = def; + try { + Class properClass = Class.forName("android.os.SystemProperties"); + Method getMethod = properClass.getMethod("get",String.class,String.class); + defVal = (String)getMethod.invoke(null,key,def); + } catch (Exception ex) { + ex.printStackTrace(); + } finally { + Log.d(TAG,"getProperty:"+key+" defVal:"+defVal); + return defVal; + } + + } + static boolean isUserVer() { + String userVer = (String)getProperties("ro.secure",null);//SystemProperties.get ( "ro.secure", "" ); + String userDebug = (String)getProperties("ro.debuggable","0");//SystemProperties.get ( "ro.debuggable", "" ); + String hideLocalUp = (String)getProperties("ro.otaupdate.local",null);//SystemProperties.get ( "ro.otaupdate.local", "" ); + if ( ( hideLocalUp != null ) && hideLocalUp.equals ( "1" ) ) { + if ( ( userVer != null ) && ( userVer.length() > 0 ) ) { + return ( userVer.trim().equals ( "1" ) ) && ( userDebug.equals ( "0" ) ); + } + } + return false; + } + + public static boolean getAutoCheck() { + String auto = (String)getProperties("ro.product.update.autocheck","false"); + return ( "true" ).equals ( auto ); + } + + public ArrayList<File> getExternalStorageList(){ + Class<?> volumeInfoC = null; + Method getvolume = null; + Method isMount = null; + Method getType = null; + Method getPath = null; + List<?> mVolumes = null; + StorageManager mStorageManager = (StorageManager)mContext.getSystemService(Context.STORAGE_SERVICE); + ArrayList<File> devList = new ArrayList<File>(); + try { + volumeInfoC = Class.forName("android.os.storage.VolumeInfo"); + getvolume = StorageManager.class.getMethod("getVolumes"); + isMount = volumeInfoC.getMethod("isMountedReadable"); + getType = volumeInfoC.getMethod("getType"); + getPath = volumeInfoC.getMethod("getPath"); + mVolumes = (List<?>)getvolume.invoke(mStorageManager); + + for (Object vol : mVolumes) { + if (vol != null && (boolean)isMount.invoke(vol) && (int)getType.invoke(vol) == 0) { + devList.add((File)getPath.invoke(vol)); + Log.d(TAG, "path.getName():" + getPath.invoke(vol)); + } + } + }catch (Exception ex) { + ex.printStackTrace(); + }finally { + return devList; + } + } + public ArrayList<File> getMainStorageList(){ + Class<?> volumeInfoC = null; + Method getvolume = null; + Method isMount = null; + Method getType = null; + Method getPath = null; + List<?> mVolumes = null; + StorageManager mStorageManager = (StorageManager)mContext.getSystemService(Context.STORAGE_SERVICE); + ArrayList<File> devList = new ArrayList<File>(); + try { + volumeInfoC = Class.forName("android.os.storage.VolumeInfo"); + getvolume = StorageManager.class.getMethod("getVolumes"); + isMount = volumeInfoC.getMethod("isMountedReadable"); + getType = volumeInfoC.getMethod("getType"); + getPath = volumeInfoC.getMethod("getPathForUser",int.class); + mVolumes = (List<?>)getvolume.invoke(mStorageManager); + + for (Object vol : mVolumes) { + if (vol != null && (boolean)isMount.invoke(vol) && ((int)getType.invoke(vol) == 0 || (int)getType.invoke(vol) == 2) ) { + devList.add((File)getPath.invoke(vol,0)); + Log.d(TAG, "path.getName():" + getPath.invoke(vol,0)); + } + } + }catch (Exception ex) { + ex.printStackTrace(); + }finally { + return devList; + } + } + public Object getDiskInfo(String filePath){ + StorageManager mStorageManager = (StorageManager)mContext.getSystemService(Context.STORAGE_SERVICE); + Class<?> volumeInfoC = null; + Class<?> deskInfoC = null; + Method getvolume = null; + Method getDisk = null; + Method isMount = null; + Method getPath = null; + Method getType = null; + List<?> mVolumes = null; + try { + volumeInfoC = Class.forName("android.os.storage.VolumeInfo"); + deskInfoC = Class.forName("android.os.storage.DiskInfo"); + getvolume = StorageManager.class.getMethod("getVolumes"); + mVolumes = (List<?>)getvolume.invoke(mStorageManager);//mStorageManager.getVolumes(); + isMount = volumeInfoC.getMethod("isMountedReadable"); + getDisk = volumeInfoC.getMethod("getDisk"); + getPath = volumeInfoC.getMethod("getPath"); + getType = volumeInfoC.getMethod("getType"); + for (Object vol : mVolumes) { + if (vol != null && (boolean)isMount.invoke(vol) && ((int)getType.invoke(vol) == 0)) { + Object info = getDisk.invoke(vol); + Log.d(TAG, "getDiskInfo" +((File)getPath.invoke(vol)).getAbsolutePath()); + if ( info != null && filePath.contains(((File)getPath.invoke(vol)).getAbsolutePath()) ) { + Log.d(TAG, "getDiskInfo path.getName():" +((File)getPath.invoke(vol)).getAbsolutePath()); + return info; + } + } + } + } catch (Exception ex) { + ex.printStackTrace(); + } + return null; + + } + + public String getTransPath(String inPath) { + String outPath = inPath; + String pathLast; + String pathVol; + int idx = -1; + int len; + Class<?> volumeInfoC = null; + Method getBestVolumeDescription = null; + Method getVolumes = null; + Method getType = null; + Method isMount = null; + Method getPath = null; + List<?> volumes = null; + StorageManager storageManager = (StorageManager)mContext.getSystemService(Context.STORAGE_SERVICE); + try { + volumeInfoC = Class.forName("android.os.storage.VolumeInfo"); + getVolumes = StorageManager.class.getMethod("getVolumes",StorageManager.class); + volumes = (List)getVolumes.invoke(storageManager); + isMount = volumeInfoC.getMethod("isMountedReadable"); + getType = volumeInfoC.getMethod("getType"); + getPath = volumeInfoC.getMethod("getPath"); + for (Object vol : volumes) { + if (vol != null && (boolean)isMount.invoke(vol) && (int)getType.invoke(vol) == 0) { + pathVol = ((File)getPath.invoke(vol)).getAbsolutePath(); + idx = inPath.indexOf(pathVol); + if (idx != -1) { + len = pathVol.length(); + pathLast = inPath.substring(idx + len); + getBestVolumeDescription = StorageManager.class.getMethod("getBestVolumeDescription",volumeInfoC); + + outPath = ((String)getBestVolumeDescription.invoke(storageManager,vol)) + pathLast; + } + } + } + } catch (Exception ex) { + } finally { + return outPath; + } + + } + private String getCanWritePath(){ + ArrayList<File> externalDevs = getExternalStorageList(); + String filePath = ""; + for ( int j = 0; (externalDevs != null) && j < externalDevs.size(); j++ ) { + File dir = externalDevs.get(j); + if ( dir.isDirectory() && dir.canWrite() ) { + filePath = dir.getAbsolutePath(); + filePath += "/"; + break; + } + } + return filePath; + } + + public int createAmlScript(String fullpath, boolean wipe_data, boolean wipe_cache) { + + File file; + String res = ""; + int UpdateMode = OtaUpgradeUtils.UPDATE_UPDATE; + file = new File("/cache/recovery/command"); + + try { + File parent = file.getParentFile(); + if (file.exists()) { + file.delete(); + } + if (!parent.exists()) { + parent.mkdirs(); + } + + if (!file.exists()) { + file.createNewFile(); + } + } catch (IOException e) { + e.printStackTrace(); + } + res += "--update_package="; + Class<?> deskInfoClass = null; + Method isSd = null; + Method isUsb = null; + Object info = getDiskInfo(fullpath); + if (info == null) { + res += "/cache/"; + UpdateMode = OtaUpgradeUtils.UPDATE_OTA; + } else { + try { + deskInfoClass = Class.forName("android.os.storage.DiskInfo"); + isSd = deskInfoClass.getMethod("isSd"); + isUsb = deskInfoClass.getMethod("isUsb"); + if ( info != null ) { + if ( (boolean)isSd.invoke(info) ) { + res += "/sdcard/"; + }else if ( (boolean)isUsb.invoke(info) ) { + res += "/udisk/"; + }else { + res += "/cache/"; + UpdateMode = OtaUpgradeUtils.UPDATE_OTA; + } + } else { + res += "/cache/"; + UpdateMode = OtaUpgradeUtils.UPDATE_OTA; + } + + } catch (Exception ex) { + ex.printStackTrace(); + res += "/cache/"; + UpdateMode = OtaUpgradeUtils.UPDATE_OTA; + } + } + res += new File(fullpath).getName(); + res += ("\n--locale=" + Locale.getDefault().toString()); + res += (wipe_data? "\n--wipe_data" : ""); + res += (wipe_cache? "\n--wipe_media" : ""); + + //res += (mWipeCache.isChecked() ? "\n--wipe_cache" : ""); + try { + FileOutputStream fout = new FileOutputStream(file); + byte[] buffer = res.getBytes(); + fout.write(buffer); + fout.close(); + } catch (IOException e) { + e.printStackTrace(); + Log.d(TAG, "IOException:" + this.getClass()); + } + return UpdateMode; + } + + void write2File() { + String flagParentPath = getCanWritePath(); + if ( flagParentPath.isEmpty() ) { + return; + } + File flagFile = new File ( flagParentPath, FlagFile ); + if ( !flagFile.exists() ) { + try { + flagFile.createNewFile(); + } catch ( IOException excep ) { + } + } + if ( !flagFile.canWrite() ) { + return; + } + + FileWriter fw = null; + try { + fw = new FileWriter ( flagFile ); + } catch ( IOException excep ) { + } + BufferedWriter output = new BufferedWriter ( fw ); + Set<String> downfiles = mPrefs.getStringSet ( PREFS_DOWNLOAD_FILELIST, + null ); + if ( ( downfiles != null ) && ( downfiles.size() > 0 ) ) { + String[] downlist = downfiles.toArray ( new String[0] ); + for ( int i = 0; i < downlist.length; i++ ) { + try { + output.write ( downlist[i] ); + output.newLine(); + } catch ( IOException ex ) { + } + } + } + try { + output.close(); + } catch ( IOException e ) { + } + } + + public void copyBackup(boolean outside){ + String backupInrFile = "/data/data/com.droidlogic.otaupgrade/BACKUP"; + String backupOutFile = getCanWritePath(); + + + File dev = new File ( backupOutFile ); + if ( !new File(backupInrFile).exists() ) { return; } + if ( backupOutFile.isEmpty() || dev == null || !dev.canWrite() ) { + return; + } + if ( dev.isDirectory() && dev.canWrite() && !dev.getName().startsWith(".") ) { + backupOutFile = dev.getAbsolutePath(); + backupOutFile += "/BACKUP"; + if ( !backupOutFile.equals ( "" ) ) { + try { + if ( outside ) + copyFile ( backupInrFile, backupOutFile ); + else + copyFile ( backupOutFile, backupInrFile); + } catch ( Exception ex ) { + ex.printStackTrace(); + } + } + } + } + + public void copyBKFile() { + copyBackup(true); + } + + + + public String onExternalPathSwitch(String filePath) { + if ( filePath.contains(EXTERNAL_STORAGE) || filePath.contains(EXTERNAL_STORAGE.toUpperCase()) ) { + String path = getCanWritePath(); + if ( path != null && !path.isEmpty() ) { + filePath = filePath.replace(EXTERNAL_STORAGE,path); + } + } + return filePath; + } + + + public static void copyFile ( String fileFromPath, String fileToPath ) throws Exception { + + FileInputStream fi = null; + FileOutputStream fo = null; + FileChannel in = null; + FileChannel out = null; + Log.d(TAG,"copyFile from "+fileFromPath+" to "+fileToPath); + try { + fi = new FileInputStream ( new File ( fileFromPath ) ); + in = fi.getChannel(); + fo = new FileOutputStream ( new File ( fileToPath ) ); + out = fo.getChannel(); + in.transferTo ( 0, in.size(), out ); + } finally { + try{ + fi.close(); + fo.close(); + in.close(); + out.close(); + }catch(Exception ex){ + } + } + } + +} |