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;
}
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);
}
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();
}
}
}
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);
}
}
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();
}
}
}
Aggregations