1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
| public class ThemeManager {
private static final int ALLAPPS = 0;
private static final int SMS = 1;
private static final int CONTACTS = 2;
private static final int BROWSER = 3;
private static final int PHONE = 4;
private static final int WPP = 5;
// Le launcher utilise les img de cet Array pour set les dockIcon
private static SparseArray<Bitmap> DicoDataBmp = new SparseArray<Bitmap>(){{
append(ALLAPPS, null);
append(SMS, null);
append(CONTACTS, null);
append(BROWSER, null);
append(PHONE, null);
append(WPP, null);
}};
// Les fichiers qui serviront à save les images en local
private static SparseArray<String> DicoDataFilename = new SparseArray<String>(){{
append(ALLAPPS, "file_allapps.jpg");
append(SMS, "file_sms.jpg");
append(CONTACTS, "file_contacts.jpg");
append(BROWSER, "file_browser.jpg");
append(PHONE, "file_phone.jpg");
append(WPP, "file_wpp.jpg");
}};
// Les images par défauts du launcher
private static SparseIntArray DicoDataDefaultDrawable = new SparseIntArray(){{
append(ALLAPPS, R.drawable.ic_allapps);
append(SMS, R.drawable.sms);
append(CONTACTS, R.drawable.contacts);
append(BROWSER, R.drawable.web);
append(PHONE, R.drawable.phone);
append(WPP, R.drawable.wallpaper_01);
}};
private final static String CURRENT_THEME_PCKG = "currentThemePackage";
public static void ApplyTheme(Context context){
// Calls to tell the launcher to set the dock icons
HelperUI.SetCustomDockToolbar(context,true);
final ArrayList<ApplicationInfo> list = (ArrayList<ApplicationInfo>) LauncherModel.sWorkspaceItems.clone();
HelperUI.SetAppList(list,true);
// Set wpp
if(DicoDataBmp.get(WPP) != null){
try {
WallpaperManager.getInstance(context).setBitmap(DicoDataBmp.get(WPP));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// Cette fonction est applée au démarrage du launcher
public static void SetTheme(Context context){
// Load images from sdcard
File sdcard = Environment.getExternalStorageDirectory();
String file;
Bitmap bmp;
int key;
for(int i=0 ; i<DicoDataFilename.size() ; i++){
key = DicoDataFilename.keyAt(i);
file = new File(sdcard + File.separator + DicoDataFilename.get(key)).getAbsolutePath();
bmp = BitmapFactory.decodeFile(file);
if(bmp == null){
DicoDataBmp.put(key, BitmapFactory.decodeResource(context.getResources(), DicoDataDefaultDrawable.get(key)));
}else{
DicoDataBmp.put(key, bmp);
}
}
ApplyTheme(context);
}
public static void SetDefaultTheme(Context context){
File sdcard = Environment.getExternalStorageDirectory();
File file;
FileOutputStream fos;
int key;
try {
for(int i=0 ; i<DicoDataFilename.size() ; i++){
key = DicoDataFilename.keyAt(i);
file = new File(sdcard + File.separator + DicoDataFilename.get(key));
fos = new FileOutputStream(file);
fos.write(0);
DicoDataBmp.put(key, BitmapFactory.decodeResource(context.getResources(), DicoDataDefaultDrawable.get(key)));
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ApplyTheme(context);
}
// Get/Set
// TODO:
// If set null, set default
public static Bitmap getIcoBrowser() {
return DicoDataBmp.get(BROWSER);
}
public static Bitmap getIcoPhone() {
return DicoDataBmp.get(PHONE);
}
public static Bitmap getIcoContacts() {
return DicoDataBmp.get(CONTACTS);
}
public static Bitmap getIcoSms() {
return DicoDataBmp.get(SMS);
}
public static Bitmap getIcoAllApps() {
return DicoDataBmp.get(ALLAPPS);
}
public static Bitmap getWpp(){
return DicoDataBmp.get(WPP);
}
public static void setIcoBrowser(Bitmap bmp) {
DicoDataBmp.put(BROWSER, bmp);
saveImageToSD(bmp, BROWSER);
}
public static void setIcoPhone(Bitmap bmp) {
DicoDataBmp.put(PHONE, bmp);
saveImageToSD(bmp, PHONE);
}
public static void setIcoContacts(Bitmap bmp) {
DicoDataBmp.put(CONTACTS, bmp);
saveImageToSD(bmp, CONTACTS);
}
public static void setIcoSms(Bitmap bmp) {
DicoDataBmp.put(SMS, bmp);
saveImageToSD(bmp, SMS);
}
public static void setIcoAllApps(Bitmap bmp) {
DicoDataBmp.put(ALLAPPS, bmp);
saveImageToSD(bmp, ALLAPPS);
}
public static void setWpp(Bitmap bmp){
DicoDataBmp.put(WPP, bmp);
saveImageToSD(bmp, WPP);
}
public static void setIcoBrowser(String url) {
Bitmap bmp = getBitmapFromURL(url);
DicoDataBmp.put(BROWSER, bmp );
saveImageToSD(bmp, BROWSER);
}
public static void setIcoPhone(String url) {
Bitmap bmp = getBitmapFromURL(url);
DicoDataBmp.put(PHONE, bmp );
saveImageToSD(bmp, PHONE);
}
public static void setIcoContacts(String url) {
Bitmap bmp = getBitmapFromURL(url);
DicoDataBmp.put(CONTACTS, bmp );
saveImageToSD(bmp, CONTACTS);
}
public static void setIcoSms(String url) {
Bitmap bmp = getBitmapFromURL(url);
DicoDataBmp.put(SMS, bmp );
saveImageToSD(bmp, SMS);
}
public static void setIcoAllApps(String url) {
Bitmap bmp = getBitmapFromURL(url);
DicoDataBmp.put(ALLAPPS, bmp );
saveImageToSD(bmp, ALLAPPS);
}
public static void setWpp(String url){
Bitmap bmp = getBitmapFromURL(url);
DicoDataBmp.put(WPP, bmp );
saveImageToSD(bmp, WPP);
}
public static Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
private static void saveImageToSD(Bitmap toSave, int data) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
toSave.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File file = new File(Environment.getExternalStorageDirectory()
+ File.separator + DicoDataFilename.get(data));
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
fos.write(bytes.toByteArray());
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} |
Partager