Search in sources :

Example 1 with CacheableBitmapDrawable

use of uk.co.senab.bitmapcache.CacheableBitmapDrawable in project Talon-for-Twitter by klinker24.

the class PhotoFragment method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    activity = getActivity();
    Bundle args = getArguments();
    url = args.getString("url");
    final View root = inflater.inflate(R.layout.photo_dialog_layout, container, false);
    picture = (NetworkedCacheableImageView) root.findViewById(R.id.picture);
    PhotoViewAttacher mAttacher = new PhotoViewAttacher(picture);
    picture.loadImage(url, false, new NetworkedCacheableImageView.OnImageLoadedListener() {

        @Override
        public void onImageLoaded(CacheableBitmapDrawable result) {
            LinearLayout spinner = (LinearLayout) root.findViewById(R.id.list_progress);
            spinner.setVisibility(View.GONE);
        }
    }, 0, // no transform
    true);
    mAttacher.setOnViewTapListener(new PhotoViewAttacher.OnViewTapListener() {

        @Override
        public void onViewTap(View view, float x, float y) {
            try {
                activity.finish();
            } catch (Exception e) {
            // activity is null
            }
        }
    });
    return root;
}
Also used : NetworkedCacheableImageView(com.klinker.android.twitter.views.NetworkedCacheableImageView) Bundle(android.os.Bundle) CacheableBitmapDrawable(uk.co.senab.bitmapcache.CacheableBitmapDrawable) NetworkedCacheableImageView(com.klinker.android.twitter.views.NetworkedCacheableImageView) View(android.view.View) PhotoViewAttacher(uk.co.senab.photoview.PhotoViewAttacher) LinearLayout(android.widget.LinearLayout)

Example 2 with CacheableBitmapDrawable

use of uk.co.senab.bitmapcache.CacheableBitmapDrawable in project Talon-for-Twitter by klinker24.

the class ImageUtils method imageUrlAsyncTask.

private static void imageUrlAsyncTask(final Context context, final ImageView imageView, final BitmapLruCache mCache, final boolean profile, final String url) {
    final WeakReference<ImageView> mImageViewRef = new WeakReference<ImageView>(imageView);
    Thread imageDownload = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                // Return early if the ImageView has disappeared.
                if (null == mImageViewRef.get()) {
                    return;
                }
                // Now we're not on the main thread we can check all caches
                CacheableBitmapDrawable result;
                result = mCache.get(url, null);
                if (null == result || profile) {
                    String mUrl = url;
                    if (url.contains("twitpic")) {
                        try {
                            URL address = new URL(url);
                            HttpURLConnection connection = (HttpURLConnection) address.openConnection(Proxy.NO_PROXY);
                            connection.setConnectTimeout(1000);
                            connection.setInstanceFollowRedirects(false);
                            connection.setReadTimeout(1000);
                            connection.connect();
                            String expandedURL = connection.getHeaderField("Location");
                            if (expandedURL != null) {
                                mUrl = expandedURL;
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    // The bitmap isn't cached so download from the web
                    HttpURLConnection conn = (HttpURLConnection) new URL(mUrl).openConnection();
                    InputStream is = new BufferedInputStream(conn.getInputStream());
                    Bitmap b = decodeSampledBitmapFromResourceMemOpt(is, 500, 500);
                    try {
                        is.close();
                    } catch (Exception e) {
                    }
                    try {
                        conn.disconnect();
                    } catch (Exception e) {
                    }
                    // Add to cache
                    try {
                        result = mCache.put(mUrl, b);
                    } catch (Exception e) {
                        result = null;
                    }
                }
                final CacheableBitmapDrawable fResult = result;
                ((Activity) context).runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            final ImageView iv = mImageViewRef.get();
                            if (null != iv && iv.getVisibility() != View.GONE) {
                                iv.setImageDrawable(fResult);
                                Animation fadeInAnimation = AnimationUtils.loadAnimation(context, R.anim.fade_in);
                                iv.startAnimation(fadeInAnimation);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
            } catch (IOException e) {
                Log.e("ImageUrlAsyncTask", e.toString());
            } catch (OutOfMemoryError e) {
                Log.v("ImageUrlAsyncTask", "Out of memory error here");
            } catch (Exception e) {
                // something else
                e.printStackTrace();
            }
        }
    });
    imageDownload.setPriority(8);
    imageDownload.start();
}
Also used : BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) URL(java.net.URL) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) IOException(java.io.IOException) Bitmap(android.graphics.Bitmap) HttpURLConnection(java.net.HttpURLConnection) BufferedInputStream(java.io.BufferedInputStream) WeakReference(java.lang.ref.WeakReference) Animation(android.view.animation.Animation) CacheableBitmapDrawable(uk.co.senab.bitmapcache.CacheableBitmapDrawable) ImageView(android.widget.ImageView)

Example 3 with CacheableBitmapDrawable

use of uk.co.senab.bitmapcache.CacheableBitmapDrawable in project Talon-for-Twitter by klinker24.

the class NotificationUtils method getImage.

public static Bitmap getImage(Context context, String screenname) {
    BitmapLruCache mCache = App.getInstance(context).getBitmapCache();
    String url;
    try {
        url = Utils.getTwitter(context, AppSettings.getInstance(context)).showUser(screenname).getBiggerProfileImageURL();
        CacheableBitmapDrawable wrapper = mCache.get(url + "_notification");
        if (wrapper == null) {
            // The bitmap isn't cached so download from the web
            HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
            InputStream is = new BufferedInputStream(conn.getInputStream());
            Bitmap image = BitmapFactory.decodeStream(is);
            image = ImageUtils.notificationResize(context, image);
            mCache.put(url + "_notification", image);
            return image;
        } else {
            return wrapper.getBitmap();
        }
    } catch (Exception e) {
        return BitmapFactory.decodeResource(context.getResources(), R.drawable.drawer_user_dark);
    }
}
Also used : BitmapLruCache(uk.co.senab.bitmapcache.BitmapLruCache) Bitmap(android.graphics.Bitmap) HttpURLConnection(java.net.HttpURLConnection) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) CacheableBitmapDrawable(uk.co.senab.bitmapcache.CacheableBitmapDrawable) URL(java.net.URL)

Example 4 with CacheableBitmapDrawable

use of uk.co.senab.bitmapcache.CacheableBitmapDrawable in project Talon-for-Twitter by klinker24.

the class PhotoViewerActivity method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    context = this;
    try {
        getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
    } catch (Exception e) {
        Log.e(LOGGER_TAG, "", e);
    }
    if (Build.VERSION.SDK_INT > 18) {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION | WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    }
    url = getIntent().getStringExtra("url");
    if (url == null) {
        finish();
        return;
    }
    // get higher quality imgur pictures
    if (url.contains("imgur")) {
        url = url.replace("t.jpg", ".jpg");
    }
    if (url.contains("insta")) {
        url = url.substring(0, url.length() - 1) + "l";
    }
    boolean fromCache = getIntent().getBooleanExtra("from_cache", true);
    boolean doRestart = getIntent().getBooleanExtra("restart", true);
    final boolean fromLauncher = getIntent().getBooleanExtra("from_launcher", false);
    AppSettings settings = new AppSettings(context);
    if (Build.VERSION.SDK_INT > 18 && settings.uiExtras) {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    }
    setContentView(R.layout.photo_dialog_layout);
    if (!doRestart || getIntent().getBooleanExtra("config_changed", false)) {
        LinearLayout spinner = (LinearLayout) findViewById(R.id.list_progress);
        spinner.setVisibility(View.GONE);
    }
    picture = (NetworkedCacheableImageView) findViewById(R.id.picture);
    PhotoViewAttacher mAttacher = new PhotoViewAttacher(picture);
    picture.loadImage(url, false, new NetworkedCacheableImageView.OnImageLoadedListener() {

        @Override
        public void onImageLoaded(CacheableBitmapDrawable result) {
            LinearLayout spinner = (LinearLayout) findViewById(R.id.list_progress);
            spinner.setVisibility(View.GONE);
        }
    }, 0, // no transform
    fromCache);
    mAttacher.setOnViewTapListener(new PhotoViewAttacher.OnViewTapListener() {

        @Override
        public void onViewTap(View view, float x, float y) {
            ((Activity) context).finish();
        }
    });
    ActionBar ab = getActionBar();
    if (ab != null) {
        ColorDrawable transparent = new ColorDrawable(getResources().getColor(android.R.color.transparent));
        ab.setBackgroundDrawable(transparent);
        ab.setDisplayHomeAsUpEnabled(false);
        ab.setDisplayShowHomeEnabled(false);
        ab.setTitle("");
        ab.setIcon(transparent);
    }
}
Also used : AppSettings(com.klinker.android.twitter.settings.AppSettings) PhotoViewAttacher(uk.co.senab.photoview.PhotoViewAttacher) NetworkedCacheableImageView(com.klinker.android.twitter.views.NetworkedCacheableImageView) View(android.view.View) NetworkedCacheableImageView(com.klinker.android.twitter.views.NetworkedCacheableImageView) ColorDrawable(android.graphics.drawable.ColorDrawable) CacheableBitmapDrawable(uk.co.senab.bitmapcache.CacheableBitmapDrawable) LinearLayout(android.widget.LinearLayout) ActionBar(android.app.ActionBar)

Example 5 with CacheableBitmapDrawable

use of uk.co.senab.bitmapcache.CacheableBitmapDrawable in project Talon-for-Twitter by klinker24.

the class ActivityCursorAdapter method bindView.

@Override
public void bindView(final View view, Context mContext, final Cursor cursor) {
    final ViewHolder holder = (ViewHolder) view.getTag();
    final String title = cursor.getString(TITLE_COL);
    final long id = cursor.getLong(TWEET_COL);
    holder.tweetId = id;
    final String profilePic = cursor.getString(PRO_PIC_COL);
    holder.proPicUrl = profilePic;
    final String tweetText = cursor.getString(TEXT_COL);
    final String name = cursor.getString(NAME_COL);
    final String screenname = cursor.getString(SCREEN_NAME_COL);
    final String picUrl = cursor.getString(PIC_COL);
    holder.picUrl = picUrl;
    final long longTime = cursor.getLong(TIME_COL);
    final String otherUrl = cursor.getString(URL_COL);
    final String users = cursor.getString(USER_COL);
    final String hashtags = cursor.getString(HASHTAG_COL);
    holder.gifUrl = cursor.getString(GIF_COL);
    String retweeter;
    try {
        retweeter = cursor.getString(RETWEETER_COL);
    } catch (Exception e) {
        retweeter = "";
    }
    if (retweeter == null) {
        retweeter = "";
    }
    holder.name.setSingleLine(true);
    int type = cursor.getInt(TYPE_COL);
    switch(type) {
        case ActivityDataSource.TYPE_NEW_FOLLOWER:
            holder.background.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    String[] userArray = users.split(" ");
                    if (userArray.length == 1) {
                        Intent viewProfile = new Intent(context, ProfilePager.class);
                        viewProfile.putExtra("screenname", userArray[0].replace("@", "").replace(" ", ""));
                        context.startActivity(viewProfile);
                    } else {
                        displayUserDialog(userArray);
                    }
                }
            });
            break;
        case ActivityDataSource.TYPE_MENTION:
            final String fRetweeter = retweeter;
            holder.background.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    if (holder.preventNextClick) {
                        holder.preventNextClick = false;
                        return;
                    }
                    String link = "";
                    boolean displayPic = !holder.picUrl.equals("") && !holder.picUrl.contains("youtube");
                    if (displayPic) {
                        link = holder.picUrl;
                    } else {
                        link = otherUrl.split("  ")[0];
                    }
                    Intent viewTweet = new Intent(context, TweetPager.class);
                    viewTweet.putExtra("name", name);
                    viewTweet.putExtra("screenname", screenname);
                    viewTweet.putExtra("time", longTime);
                    viewTweet.putExtra("tweet", tweetText);
                    viewTweet.putExtra("retweeter", fRetweeter);
                    viewTweet.putExtra("webpage", link);
                    viewTweet.putExtra("picture", displayPic);
                    viewTweet.putExtra("other_links", otherUrl);
                    viewTweet.putExtra("tweetid", holder.tweetId);
                    viewTweet.putExtra("proPic", profilePic);
                    viewTweet.putExtra("users", users);
                    viewTweet.putExtra("hashtags", hashtags);
                    viewTweet.putExtra("animated_gif", holder.gifUrl);
                    context.startActivity(viewTweet);
                }
            });
            break;
        case ActivityDataSource.TYPE_FAVORITES:
        case ActivityDataSource.TYPE_RETWEETS:
            holder.background.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    Intent retweeters = new Intent(context, ViewUsersPopup.class);
                    retweeters.putExtra("id", id);
                    context.startActivity(retweeters);
                }
            });
            break;
    }
    holder.profilePic.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            Intent viewProfile = new Intent(context, ProfilePager.class);
            if (screenname == null) {
                String[] userArray = users.split(" ");
                viewProfile.putExtra("screenname", userArray[0].replace("@", "").replace(" ", ""));
            } else if (screenname.contains(" ")) {
                holder.background.performClick();
            } else {
                viewProfile.putExtra("screenname", screenname);
            }
            context.startActivity(viewProfile);
        }
    });
    holder.name.setText(title);
    holder.tweet.setText(tweetText);
    CacheableBitmapDrawable wrapper2 = mCache.getFromMemoryCache(holder.proPicUrl);
    final boolean gotProPic;
    if (wrapper2 == null) {
        gotProPic = false;
        if (holder.profilePic.getDrawable() != null) {
            holder.profilePic.setImageDrawable(null);
        }
    } else {
        gotProPic = true;
        holder.profilePic.setImageDrawable(wrapper2);
    }
    mHandlers[currHandler].postDelayed(new Runnable() {

        @Override
        public void run() {
            if (holder.tweetId == id) {
                if (!gotProPic) {
                    loadProPic(context, holder, holder.proPicUrl, mCache, id);
                }
                if (settings.useEmoji && (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT || EmojiUtils.ios)) {
                    String text = holder.tweet.getText().toString();
                    if (EmojiUtils.emojiPattern.matcher(text).find()) {
                        final Spannable span = EmojiUtils.getSmiledText(context, Html.fromHtml(tweetText));
                        holder.tweet.setText(span);
                    }
                }
                holder.tweet.setSoundEffectsEnabled(false);
                holder.tweet.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        if (!TouchableMovementMethod.touched) {
                            // absorbs the click on the background
                            if (!holder.preventNextClick) {
                                holder.background.getBackground().setState(new int[] { android.R.attr.state_pressed });
                                new Handler().postDelayed(new Runnable() {

                                    @Override
                                    public void run() {
                                        holder.background.getBackground().setState(new int[] { android.R.attr.state_empty });
                                    }
                                }, 25);
                            }
                            holder.background.performClick();
                        }
                    }
                });
                holder.tweet.setOnLongClickListener(new View.OnLongClickListener() {

                    @Override
                    public boolean onLongClick(View view) {
                        if (!TouchableMovementMethod.touched) {
                            holder.background.performLongClick();
                            holder.preventNextClick = true;
                        }
                        return false;
                    }
                });
                TextUtils.linkifyText(context, holder.tweet, holder.background, true, otherUrl, false);
                TextUtils.linkifyText(context, holder.retweeter, holder.background, true, "", false);
            }
        }
    }, 400);
    currHandler++;
    if (currHandler == 10) {
        currHandler = 0;
    }
}
Also used : TweetPager(com.klinker.android.twitter.activities.tweet_viewer.TweetPager) Handler(android.os.Handler) Intent(android.content.Intent) View(android.view.View) ViewUsersPopup(com.klinker.android.twitter.activities.tweet_viewer.users_popup.ViewUsersPopup) ProfilePager(com.klinker.android.twitter.activities.profile_viewer.ProfilePager) CacheableBitmapDrawable(uk.co.senab.bitmapcache.CacheableBitmapDrawable) Spannable(android.text.Spannable)

Aggregations

CacheableBitmapDrawable (uk.co.senab.bitmapcache.CacheableBitmapDrawable)16 View (android.view.View)9 NetworkedCacheableImageView (com.klinker.android.twitter.views.NetworkedCacheableImageView)8 Intent (android.content.Intent)7 Bitmap (android.graphics.Bitmap)7 URL (java.net.URL)7 TweetPager (com.klinker.android.twitter.activities.tweet_viewer.TweetPager)5 BufferedInputStream (java.io.BufferedInputStream)5 InputStream (java.io.InputStream)5 HttpURLConnection (java.net.HttpURLConnection)5 Handler (android.os.Handler)3 Spannable (android.text.Spannable)3 ImageView (android.widget.ImageView)3 PhotoViewerActivity (com.klinker.android.twitter.activities.photo_viewer.PhotoViewerActivity)3 ProfilePager (com.klinker.android.twitter.activities.profile_viewer.ProfilePager)3 LayoutInflater (android.view.LayoutInflater)2 AbsListView (android.widget.AbsListView)2 LinearLayout (android.widget.LinearLayout)2 TextView (android.widget.TextView)2 PhotoPagerActivity (com.klinker.android.twitter.activities.photo_viewer.PhotoPagerActivity)2