修訂 | ef4db618691d8521279ad98fd5d829e9c63d81c7 (tree) |
---|---|
時間 | 2017-08-21 01:36:34 |
作者 | HMML <hmml3939@gmai...> |
Commiter | HMML |
Fix delayed image loading with MikuWeater icons.
@@ -1,19 +1,60 @@ | ||
1 | 1 | package cloud.hmml.mmw; |
2 | 2 | |
3 | 3 | import android.app.Application; |
4 | +import android.content.Context; | |
5 | +import android.content.pm.PackageManager; | |
6 | +import android.content.res.Resources; | |
7 | +import android.graphics.Bitmap; | |
8 | +import android.graphics.BitmapFactory; | |
9 | +import android.net.Uri; | |
4 | 10 | |
5 | 11 | import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache; |
6 | 12 | import com.nostra13.universalimageloader.core.ImageLoader; |
7 | 13 | import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; |
14 | +import com.nostra13.universalimageloader.core.decode.ImageDecoder; | |
15 | +import com.nostra13.universalimageloader.core.decode.ImageDecodingInfo; | |
16 | +import com.nostra13.universalimageloader.core.download.BaseImageDownloader; | |
17 | + | |
18 | +import java.io.IOException; | |
19 | +import java.io.InputStream; | |
8 | 20 | |
9 | 21 | |
10 | 22 | public class MainApplication extends Application { |
23 | + class MmwImageDownloader extends BaseImageDownloader { | |
24 | + public MmwImageDownloader(Context context) { | |
25 | + super(context); | |
26 | + } | |
27 | + | |
28 | + @Override | |
29 | + public InputStream getStream(String imageUri, Object extra) throws IOException { | |
30 | + if (Uri.parse(imageUri).getScheme().equals("mikuweather-icon")) | |
31 | + return getStreamFromMikuWeather(imageUri, extra); | |
32 | + return super.getStream(imageUri, extra); | |
33 | + } | |
34 | + | |
35 | + public InputStream getStreamFromMikuWeather(String imageUri, Object extra) throws IOException { | |
36 | + Context mx_context = null; | |
37 | + Uri uri = Uri.parse(imageUri); | |
38 | + try { | |
39 | + mx_context = context.createPackageContext("com.mikuxperia.mikuweatherwidget", Context.CONTEXT_RESTRICTED); | |
40 | + } catch (PackageManager.NameNotFoundException e) { | |
41 | + throw new IOException("Faeild to resolv miku weather package"); | |
42 | + } | |
43 | + Resources resources = mx_context.getResources(); | |
44 | + int resourceId = resources.getIdentifier(uri.getSchemeSpecificPart(), "drawable", "com.mikuxperia.mikuweatherwidget"); | |
45 | + return resources.openRawResource(resourceId); | |
46 | + } | |
47 | + | |
48 | + } | |
49 | + | |
50 | + | |
11 | 51 | @Override |
12 | 52 | public void onCreate() { |
13 | 53 | super.onCreate(); |
14 | 54 | ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext()) |
15 | 55 | .memoryCache(new LruMemoryCache(5 * 1024 * 1024)) |
16 | 56 | .memoryCacheSize(5 * 1024 * 1024) |
57 | + .imageDownloader(new MmwImageDownloader(getApplicationContext())) | |
17 | 58 | .build(); |
18 | 59 | ImageLoader.getInstance().init(config); |
19 | 60 |
@@ -232,6 +232,7 @@ public class Theme { | ||
232 | 232 | try { |
233 | 233 | theme = JSON.decode(istream, Theme.class); |
234 | 234 | } catch (IOException e) { |
235 | + Log.e("theme", "Failed to decode theme definition json: " + e.getMessage()); | |
235 | 236 | e.printStackTrace(); |
236 | 237 | return null; |
237 | 238 | } |
@@ -246,7 +247,10 @@ public class Theme { | ||
246 | 247 | |
247 | 248 | public String getIconURL(String iconIdent) { |
248 | 249 | if (type == TYPE_MIKU_WEATHER) { |
249 | - return null; | |
250 | + String icon_name = MW_RES_MAP.get(iconIdent); | |
251 | + if (icon_name == null) | |
252 | + return null; | |
253 | + return "mikuweather-icon:tenki_"+icon_name; | |
250 | 254 | } else if (type == TYPE_BUILTIN) { |
251 | 255 | return "assets://themes/" + id + "/" + W_FNAME_MAP.get(iconIdent); |
252 | 256 | } else if (type == TYPE_IN_STORAGE) { |
@@ -318,5 +322,4 @@ public class Theme { | ||
318 | 322 | return null; |
319 | 323 | } |
320 | 324 | } |
321 | - | |
322 | 325 | } |
@@ -3,14 +3,17 @@ package cloud.hmml.mmw; | ||
3 | 3 | import android.content.Context; |
4 | 4 | import android.content.Intent; |
5 | 5 | import android.content.res.Resources; |
6 | +import android.graphics.Bitmap; | |
6 | 7 | import android.graphics.Color; |
7 | 8 | import android.graphics.drawable.Drawable; |
8 | 9 | import android.net.Uri; |
10 | +import android.os.AsyncTask; | |
9 | 11 | import android.os.Bundle; |
10 | 12 | import android.support.annotation.LayoutRes; |
11 | 13 | import android.support.annotation.NonNull; |
12 | 14 | import android.support.design.widget.FloatingActionButton; |
13 | 15 | import android.support.design.widget.Snackbar; |
16 | +import android.support.v4.content.AsyncTaskLoader; | |
14 | 17 | import android.support.v4.content.ContextCompat; |
15 | 18 | import android.support.v7.app.AppCompatActivity; |
16 | 19 | import android.support.v7.widget.Toolbar; |
@@ -26,6 +29,8 @@ import android.widget.TextView; | ||
26 | 29 | |
27 | 30 | import com.nostra13.universalimageloader.core.ImageLoader; |
28 | 31 | import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; |
32 | +import com.nostra13.universalimageloader.core.assist.FailReason; | |
33 | +import com.nostra13.universalimageloader.core.listener.ImageLoadingListener; | |
29 | 34 | |
30 | 35 | import java.util.List; |
31 | 36 |
@@ -37,7 +42,7 @@ public class ThemePickerActivity extends AppCompatActivity { | ||
37 | 42 | } |
38 | 43 | |
39 | 44 | @Override |
40 | - public View getView(int position, View convertView, ViewGroup parent) { | |
45 | + public View getView(final int position, View convertView, ViewGroup parent) { | |
41 | 46 | final Theme theme = (Theme) getItem(position); |
42 | 47 | |
43 | 48 | // Check if an existing view is being reused, otherwise inflate the view |
@@ -52,19 +57,14 @@ public class ThemePickerActivity extends AppCompatActivity { | ||
52 | 57 | ImageView image2 = (ImageView) convertView.findViewById(R.id.theme_image2); |
53 | 58 | ImageView image3 = (ImageView) convertView.findViewById(R.id.theme_image3); |
54 | 59 | |
60 | + | |
55 | 61 | // Populate the data into the template view using the data object |
56 | 62 | name.setText(theme.name); |
57 | 63 | author.setText(theme.author); |
58 | - if (theme.getType() == Theme.TYPE_MIKU_WEATHER) { | |
59 | - image1.setImageBitmap(theme.getIcon(Theme.W_CLEAR_D + Theme.W_RAIN)); | |
60 | - image2.setImageBitmap(theme.getIcon(Theme.W_SNOW + Theme.W_CLEAR_N)); | |
61 | - image3.setImageBitmap(theme.getIcon(Theme.W_CLOUD + Theme.W_THUNDER)); | |
62 | - } else { | |
63 | - ImageLoader imageLoader = ImageLoader.getInstance(); | |
64 | - imageLoader.displayImage(theme.getIconURL(Theme.W_CLEAR_D + Theme.W_RAIN), image1); | |
65 | - imageLoader.displayImage(theme.getIconURL(Theme.W_SNOW + Theme.W_CLEAR_N), image2); | |
66 | - imageLoader.displayImage(theme.getIconURL(Theme.W_CLOUD + Theme.W_THUNDER), image3); | |
67 | - } | |
64 | + ImageLoader imageLoader = ImageLoader.getInstance(); | |
65 | + imageLoader.displayImage(theme.getIconURL(Theme.W_CLEAR_D + Theme.W_RAIN), image1); | |
66 | + imageLoader.displayImage(theme.getIconURL(Theme.W_SNOW + Theme.W_CLEAR_N), image2); | |
67 | + imageLoader.displayImage(theme.getIconURL(Theme.W_CLOUD + Theme.W_THUNDER), image3); | |
68 | 68 | image2.setVisibility(View.VISIBLE); |
69 | 69 | image3.setVisibility(View.VISIBLE); |
70 | 70 |
@@ -78,7 +78,6 @@ public class ThemePickerActivity extends AppCompatActivity { | ||
78 | 78 | image2.setBackground(bg); |
79 | 79 | image3.setBackground(bg); |
80 | 80 | |
81 | - | |
82 | 81 | if (theme.getUri() != null) { |
83 | 82 | author.setTextColor(Color.BLUE); |
84 | 83 | author.setOnClickListener(new View.OnClickListener() { |