Search in sources :

Example 66 with ContextWrapper

use of android.content.ContextWrapper in project frostwire by frostwire.

the class MusicUtils method bindToService.

/**
 * @param context  The {@link Context} to use
 * @param callback The {@link ServiceConnection} to use
 * @return The new instance of {@link ServiceToken}
 */
public static ServiceToken bindToService(final Context context, final ServiceConnection callback) {
    if (context == null) {
        return null;
    }
    Activity realActivity = ((Activity) context).getParent();
    if (realActivity == null) {
        realActivity = (Activity) context;
    }
    final ContextWrapper contextWrapper = new ContextWrapper(realActivity);
    contextWrapper.startService(new Intent(contextWrapper, MusicPlaybackService.class));
    final ServiceBinder binder = new ServiceBinder(callback);
    if (contextWrapper.bindService(new Intent().setClass(contextWrapper, MusicPlaybackService.class), binder, 0)) {
        mConnectionMap.put(contextWrapper, binder);
        return new ServiceToken(contextWrapper);
    }
    return null;
}
Also used : Activity(android.app.Activity) Intent(android.content.Intent) MusicPlaybackService(com.andrew.apollo.MusicPlaybackService) ContextWrapper(android.content.ContextWrapper)

Example 67 with ContextWrapper

use of android.content.ContextWrapper in project android_frameworks_base by crdroidandroid.

the class ModelTest method setupTestContext.

private void setupTestContext() {
    final MockContentResolver resolver = new MockContentResolver();
    context = new ContextWrapper(getContext()) {

        @Override
        public ContentResolver getContentResolver() {
            return resolver;
        }
    };
    provider = new TestContentProvider();
    resolver.addProvider(AUTHORITY, provider);
}
Also used : MockContentResolver(android.test.mock.MockContentResolver) ContextWrapper(android.content.ContextWrapper) ContentResolver(android.content.ContentResolver) MockContentResolver(android.test.mock.MockContentResolver)

Example 68 with ContextWrapper

use of android.content.ContextWrapper in project Slide by ccrama.

the class HeaderImageLinkView method onLinkLongClick.

public void onLinkLongClick(final String url, MotionEvent event) {
    popped = false;
    if (url == null) {
        return;
    }
    performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
    Activity activity = null;
    final Context context = getContext();
    if (context instanceof Activity) {
        activity = (Activity) context;
    } else if (context instanceof android.support.v7.view.ContextThemeWrapper) {
        activity = (Activity) ((android.support.v7.view.ContextThemeWrapper) context).getBaseContext();
    } else if (context instanceof ContextWrapper) {
        Context context1 = ((ContextWrapper) context).getBaseContext();
        if (context1 instanceof Activity) {
            activity = (Activity) context1;
        } else if (context1 instanceof ContextWrapper) {
            Context context2 = ((ContextWrapper) context1).getBaseContext();
            if (context2 instanceof Activity) {
                activity = (Activity) context2;
            } else if (context2 instanceof ContextWrapper) {
                activity = (Activity) ((android.support.v7.view.ContextThemeWrapper) context2).getBaseContext();
            }
        }
    } else {
        throw new RuntimeException("Could not find activity from context:" + context);
    }
    if (activity != null && !activity.isFinishing()) {
        if (SettingValues.peek) {
            Peek.into(R.layout.peek_view_submission, new SimpleOnPeek() {

                @Override
                public void onInflated(final PeekView peekView, final View rootView) {
                    // do stuff
                    TextView text = ((TextView) rootView.findViewById(R.id.title));
                    text.setText(url);
                    text.setTextColor(Color.WHITE);
                    ((PeekMediaView) rootView.findViewById(R.id.peek)).setUrl(url);
                    peekView.addButton((R.id.share), new OnButtonUp() {

                        @Override
                        public void onButtonUp() {
                            Reddit.defaultShareText("", url, rootView.getContext());
                        }
                    });
                    peekView.addButton((R.id.upvoteb), new OnButtonUp() {

                        @Override
                        public void onButtonUp() {
                            ((View) getParent()).findViewById(R.id.upvote).callOnClick();
                        }
                    });
                    peekView.setOnRemoveListener(new OnRemove() {

                        @Override
                        public void onRemove() {
                            ((PeekMediaView) rootView.findViewById(R.id.peek)).doClose();
                        }
                    });
                    peekView.addButton((R.id.comments), new OnButtonUp() {

                        @Override
                        public void onButtonUp() {
                            ((View) getParent().getParent()).callOnClick();
                        }
                    });
                    peekView.setOnPop(new OnPop() {

                        @Override
                        public void onPop() {
                            popped = true;
                            callOnClick();
                        }
                    });
                }
            }).with(new PeekViewOptions().setFullScreenPeek(true)).show((PeekViewActivity) activity, event);
        } else {
            BottomSheet.Builder b = new BottomSheet.Builder(activity).title(url).grid();
            int[] attrs = new int[] { R.attr.tintColor };
            TypedArray ta = getContext().obtainStyledAttributes(attrs);
            int color = ta.getColor(0, Color.WHITE);
            Drawable open = getResources().getDrawable(R.drawable.ic_open_in_browser);
            open.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
            Drawable share = getResources().getDrawable(R.drawable.ic_share);
            share.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
            Drawable copy = getResources().getDrawable(R.drawable.ic_content_copy);
            copy.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
            ta.recycle();
            b.sheet(R.id.open_link, open, getResources().getString(R.string.submission_link_extern));
            b.sheet(R.id.share_link, share, getResources().getString(R.string.share_link));
            b.sheet(R.id.copy_link, copy, getResources().getString(R.string.submission_link_copy));
            final Activity finalActivity = activity;
            b.listener(new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    switch(which) {
                        case R.id.open_link:
                            LinkUtil.openExternally(url, context);
                            break;
                        case R.id.share_link:
                            Reddit.defaultShareText("", url, finalActivity);
                            break;
                        case R.id.copy_link:
                            LinkUtil.copyUrl(url, finalActivity);
                            break;
                    }
                }
            }).show();
        }
    }
}
Also used : DialogInterface(android.content.DialogInterface) PeekViewActivity(me.ccrama.redditslide.ForceTouch.PeekViewActivity) Activity(android.app.Activity) PeekMediaView(me.ccrama.redditslide.Views.PeekMediaView) TypedArray(android.content.res.TypedArray) PeekView(me.ccrama.redditslide.ForceTouch.PeekView) TransparentTagTextView(me.ccrama.redditslide.Views.TransparentTagTextView) TextView(android.widget.TextView) OnPop(me.ccrama.redditslide.ForceTouch.callback.OnPop) PeekViewOptions(me.ccrama.redditslide.ForceTouch.builder.PeekViewOptions) Context(android.content.Context) Drawable(android.graphics.drawable.Drawable) ImageView(android.widget.ImageView) PeekView(me.ccrama.redditslide.ForceTouch.PeekView) View(android.view.View) TransparentTagTextView(me.ccrama.redditslide.Views.TransparentTagTextView) TextView(android.widget.TextView) PeekMediaView(me.ccrama.redditslide.Views.PeekMediaView) OnButtonUp(me.ccrama.redditslide.ForceTouch.callback.OnButtonUp) OnRemove(me.ccrama.redditslide.ForceTouch.callback.OnRemove) BottomSheet(com.cocosw.bottomsheet.BottomSheet) ContextWrapper(android.content.ContextWrapper) SimpleOnPeek(me.ccrama.redditslide.ForceTouch.callback.SimpleOnPeek)

Example 69 with ContextWrapper

use of android.content.ContextWrapper in project Slide by ccrama.

the class SpoilerRobotoTextView method onLinkClick.

@Override
public void onLinkClick(String url, int xOffset, String subreddit, URLSpan span) {
    if (url == null) {
        ((View) getParent()).callOnClick();
        return;
    }
    ContentType.Type type = ContentType.getContentType(url);
    Context context = getContext();
    Activity activity = null;
    if (context instanceof Activity) {
        activity = (Activity) context;
    } else if (context instanceof android.support.v7.view.ContextThemeWrapper) {
        activity = (Activity) ((android.support.v7.view.ContextThemeWrapper) context).getBaseContext();
    } else if (context instanceof ContextWrapper) {
        Context context1 = ((ContextWrapper) context).getBaseContext();
        if (context1 instanceof Activity) {
            activity = (Activity) context1;
        } else if (context1 instanceof ContextWrapper) {
            Context context2 = ((ContextWrapper) context1).getBaseContext();
            if (context2 instanceof Activity) {
                activity = (Activity) context2;
            } else if (context2 instanceof ContextWrapper) {
                activity = (Activity) ((android.support.v7.view.ContextThemeWrapper) context2).getBaseContext();
            }
        }
    } else {
        throw new RuntimeException("Could not find activity from context:" + context);
    }
    if (!PostMatch.openExternal(url) || type == ContentType.Type.VIDEO) {
        switch(type) {
            case DEVIANTART:
            case IMGUR:
            case XKCD:
                if (SettingValues.image) {
                    Intent intent2 = new Intent(activity, MediaView.class);
                    intent2.putExtra(MediaView.EXTRA_URL, url);
                    intent2.putExtra(MediaView.SUBREDDIT, subreddit);
                    activity.startActivity(intent2);
                } else {
                    LinkUtil.openExternally(url, activity);
                }
                break;
            case REDDIT:
                new OpenRedditLink(activity, url);
                break;
            case LINK:
                LogUtil.v("Opening link");
                LinkUtil.openUrl(url, Palette.getColor(subreddit), activity);
                break;
            case SELF:
                break;
            case STREAMABLE:
            case VID_ME:
                openStreamable(url, subreddit);
                break;
            case ALBUM:
                if (SettingValues.album) {
                    if (SettingValues.albumSwipe) {
                        Intent i = new Intent(activity, AlbumPager.class);
                        i.putExtra(Album.EXTRA_URL, url);
                        i.putExtra(AlbumPager.SUBREDDIT, subreddit);
                        activity.startActivity(i);
                    } else {
                        Intent i = new Intent(activity, Album.class);
                        i.putExtra(Album.SUBREDDIT, subreddit);
                        i.putExtra(Album.EXTRA_URL, url);
                        activity.startActivity(i);
                    }
                } else {
                    LinkUtil.openExternally(url, activity);
                }
                break;
            case TUMBLR:
                if (SettingValues.image) {
                    if (SettingValues.albumSwipe) {
                        Intent i = new Intent(activity, TumblrPager.class);
                        i.putExtra(Album.EXTRA_URL, url);
                        activity.startActivity(i);
                    } else {
                        Intent i = new Intent(activity, TumblrPager.class);
                        i.putExtra(Album.EXTRA_URL, url);
                        activity.startActivity(i);
                    }
                } else {
                    LinkUtil.openExternally(url, activity);
                }
                break;
            case IMAGE:
                openImage(url, subreddit);
                break;
            case VREDDIT_REDIRECT:
                openVReddit(url, subreddit, activity);
                break;
            case GIF:
            case VREDDIT_DIRECT:
                openGif(url, subreddit, activity);
                break;
            case NONE:
                break;
            case VIDEO:
                if (Reddit.videoPlugin) {
                    try {
                        Intent sharingIntent = new Intent(Intent.ACTION_SEND);
                        sharingIntent.setClassName("ccrama.me.slideyoutubeplugin", "ccrama.me.slideyoutubeplugin.YouTubeView");
                        sharingIntent.putExtra("url", url);
                        activity.startActivity(sharingIntent);
                    } catch (Exception e) {
                        LinkUtil.openExternally(url, activity);
                    }
                } else {
                    LinkUtil.openExternally(url, activity);
                }
            case SPOILER:
                spoilerClicked = true;
                setOrRemoveSpoilerSpans(xOffset, span);
                break;
            case EXTERNAL:
                LinkUtil.openExternally(url, activity);
                break;
        }
    } else {
        LinkUtil.openExternally(url, context);
    }
}
Also used : Context(android.content.Context) PeekViewActivity(me.ccrama.redditslide.ForceTouch.PeekViewActivity) Activity(android.app.Activity) Intent(android.content.Intent) MediaView(me.ccrama.redditslide.Activities.MediaView) PeekView(me.ccrama.redditslide.ForceTouch.PeekView) View(android.view.View) TextView(android.widget.TextView) PeekMediaView(me.ccrama.redditslide.Views.PeekMediaView) RobotoTextView(com.devspark.robototextview.widget.RobotoTextView) ContextWrapper(android.content.ContextWrapper)

Example 70 with ContextWrapper

use of android.content.ContextWrapper in project Slide by ccrama.

the class SpoilerRobotoTextView method onLinkLongClick.

@Override
public void onLinkLongClick(final String baseUrl, MotionEvent event) {
    if (baseUrl == null) {
        return;
    }
    final String url = StringEscapeUtils.unescapeHtml4(baseUrl);
    performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
    Activity activity = null;
    final Context context = getContext();
    if (context instanceof Activity) {
        activity = (Activity) context;
    } else if (context instanceof android.support.v7.view.ContextThemeWrapper) {
        activity = (Activity) ((android.support.v7.view.ContextThemeWrapper) context).getBaseContext();
    } else if (context instanceof ContextWrapper) {
        Context context1 = ((ContextWrapper) context).getBaseContext();
        if (context1 instanceof Activity) {
            activity = (Activity) context1;
        } else if (context1 instanceof ContextWrapper) {
            Context context2 = ((ContextWrapper) context1).getBaseContext();
            if (context2 instanceof Activity) {
                activity = (Activity) context2;
            } else if (context2 instanceof ContextWrapper) {
                activity = (Activity) ((android.support.v7.view.ContextThemeWrapper) context2).getBaseContext();
            }
        }
    } else {
        throw new RuntimeException("Could not find activity from context:" + context);
    }
    if (activity != null && !activity.isFinishing()) {
        if (SettingValues.peek) {
            Peek.into(R.layout.peek_view, new SimpleOnPeek() {

                @Override
                public void onInflated(final PeekView peekView, final View rootView) {
                    // do stuff
                    TextView text = ((TextView) rootView.findViewById(R.id.title));
                    text.setText(url);
                    text.setTextColor(Color.WHITE);
                    ((PeekMediaView) rootView.findViewById(R.id.peek)).setUrl(url);
                    peekView.addButton((R.id.copy), new OnButtonUp() {

                        @Override
                        public void onButtonUp() {
                            ClipboardManager clipboard = (ClipboardManager) rootView.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
                            ClipData clip = ClipData.newPlainText("Link", url);
                            clipboard.setPrimaryClip(clip);
                            Toast.makeText(rootView.getContext(), R.string.submission_link_copied, Toast.LENGTH_SHORT).show();
                        }
                    });
                    peekView.setOnRemoveListener(new OnRemove() {

                        @Override
                        public void onRemove() {
                            ((PeekMediaView) rootView.findViewById(R.id.peek)).doClose();
                        }
                    });
                    peekView.addButton((R.id.share), new OnButtonUp() {

                        @Override
                        public void onButtonUp() {
                            Reddit.defaultShareText("", url, rootView.getContext());
                        }
                    });
                    peekView.addButton((R.id.pop), new OnButtonUp() {

                        @Override
                        public void onButtonUp() {
                            Reddit.defaultShareText("", url, rootView.getContext());
                        }
                    });
                    peekView.addButton((R.id.external), new OnButtonUp() {

                        @Override
                        public void onButtonUp() {
                            LinkUtil.openExternally(url, context);
                        }
                    });
                    peekView.setOnPop(new OnPop() {

                        @Override
                        public void onPop() {
                            onLinkClick(url, 0, "", null);
                        }
                    });
                }
            }).with(new PeekViewOptions().setFullScreenPeek(true)).show((PeekViewActivity) activity, event);
        } else {
            BottomSheet.Builder b = new BottomSheet.Builder(activity).title(url).grid();
            int[] attrs = new int[] { R.attr.tintColor };
            TypedArray ta = getContext().obtainStyledAttributes(attrs);
            int color = ta.getColor(0, Color.WHITE);
            Drawable open = getResources().getDrawable(R.drawable.ic_open_in_browser);
            open.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
            Drawable share = getResources().getDrawable(R.drawable.ic_share);
            share.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
            Drawable copy = getResources().getDrawable(R.drawable.ic_content_copy);
            copy.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
            ta.recycle();
            b.sheet(R.id.open_link, open, getResources().getString(R.string.submission_link_extern));
            b.sheet(R.id.share_link, share, getResources().getString(R.string.share_link));
            b.sheet(R.id.copy_link, copy, getResources().getString(R.string.submission_link_copy));
            final Activity finalActivity = activity;
            b.listener(new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    switch(which) {
                        case R.id.open_link:
                            LinkUtil.openExternally(url, context);
                            break;
                        case R.id.share_link:
                            Reddit.defaultShareText("", url, finalActivity);
                            break;
                        case R.id.copy_link:
                            LinkUtil.copyUrl(url, finalActivity);
                            break;
                    }
                }
            }).show();
        }
    }
}
Also used : DialogInterface(android.content.DialogInterface) SpannableStringBuilder(android.text.SpannableStringBuilder) PeekViewActivity(me.ccrama.redditslide.ForceTouch.PeekViewActivity) Activity(android.app.Activity) PeekMediaView(me.ccrama.redditslide.Views.PeekMediaView) TypedArray(android.content.res.TypedArray) PeekView(me.ccrama.redditslide.ForceTouch.PeekView) TextView(android.widget.TextView) RobotoTextView(com.devspark.robototextview.widget.RobotoTextView) OnPop(me.ccrama.redditslide.ForceTouch.callback.OnPop) PeekViewOptions(me.ccrama.redditslide.ForceTouch.builder.PeekViewOptions) Context(android.content.Context) ClipboardManager(android.content.ClipboardManager) Drawable(android.graphics.drawable.Drawable) MediaView(me.ccrama.redditslide.Activities.MediaView) PeekView(me.ccrama.redditslide.ForceTouch.PeekView) View(android.view.View) TextView(android.widget.TextView) PeekMediaView(me.ccrama.redditslide.Views.PeekMediaView) RobotoTextView(com.devspark.robototextview.widget.RobotoTextView) OnButtonUp(me.ccrama.redditslide.ForceTouch.callback.OnButtonUp) TextPaint(android.text.TextPaint) OnRemove(me.ccrama.redditslide.ForceTouch.callback.OnRemove) BottomSheet(com.cocosw.bottomsheet.BottomSheet) ContextWrapper(android.content.ContextWrapper) ClipData(android.content.ClipData) SimpleOnPeek(me.ccrama.redditslide.ForceTouch.callback.SimpleOnPeek)

Aggregations

ContextWrapper (android.content.ContextWrapper)109 Context (android.content.Context)37 Activity (android.app.Activity)25 File (java.io.File)16 Test (org.junit.Test)13 Intent (android.content.Intent)11 MockContentResolver (android.test.mock.MockContentResolver)11 PackageManager (android.content.pm.PackageManager)10 View (android.view.View)10 Resources (android.content.res.Resources)9 IOException (java.io.IOException)8 Before (org.junit.Before)8 ContentResolver (android.content.ContentResolver)6 UserManager (android.os.UserManager)6 LayoutInflater (android.view.LayoutInflater)6 ThemePreferenceController (com.android.settings.display.ThemePreferenceController)6 OverlayManager (com.android.settings.display.ThemePreferenceController.OverlayManager)6 ArrayList (java.util.ArrayList)6 AppWidgetHostView (android.appwidget.AppWidgetHostView)5 JsonParser (com.google.gson.JsonParser)5