Listening to default activity transition - android

I'm trying to add TransitionListener to default activity transition like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().getEnterTransition().addListener(new TransitionAdapter());
}
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
private class TransitionAdapter implements Transition.TransitionListener {
#Override
public void onTransitionStart(Transition transition) {
Log.i("transition", "onTransitionStart");
}
#Override
public void onTransitionEnd(Transition transition) {
Log.i("transition", "onTransitionEnd");
}
#Override
public void onTransitionCancel(Transition transition) {
Log.i("transition", "onTransitionCancel");
}
#Override
public void onTransitionPause(Transition transition) {
Log.i("transition", "onTransitionPause");
}
#Override
public void onTransitionResume(Transition transition) {
Log.i("transition", "onTransitionResume");
}
}
This how I start activity:
Intent intent = new Intent(activity, LoginActivity.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Bundle options = ActivityOptions.makeSceneTransitionAnimation(activity).toBundle();
activity.startActivityForResult(intent, RequestCodes.SIGN_IN, options);
} else {
activity.startActivityForResult(intent, RequestCodes.SIGN_IN);
}
The problem is no callback is ever called on real device. It works on genymotion though. Is there some additional setup required for that?
DISCLAIMER: I test it on lolipop running device

Found out it doesn't work on 5.0 but got fixed on 5.1 so it's clearly an android bug. I don't know a workaround for this though. I've restricted transitions to minimum API 22.

Related

Android Your app(s) are vulnerable to Intent Redirection

I got an email from google play support saying "Intent Redirection Your app(s) are vulnerable to Intent Redirection. To address this issue, follow the steps in this Google Help Center article."
After reading through the article, I'm guessing the key is my app should not call startActivity, startService, sendBroadcast, or setResult on untrusted Intents (intents used by external apps to invoke my app for example) without validating or sanitizing these Intents.
However, solution 1 in the article doesn't work in my case because my component needs to receive Intents from other apps.
Solution 2 is not applicable to my case because I don't know in advance which app would invoke my app, so I don't know what would getCallingActivity returns.
Solution 3 seems to be the most promising one, I tried to removeFlags of intents, however, when I resubmit my app, Google Play again alerts this vulnerability. I am about to try checking whether an Intent grants a URI permission using methods like getFlags and submit my app again to see the result. Does anyone know how do Google check this vulnerability anyway, and could someone spot the vulnerability in my source code and suggest a way to resolve it?
The exact message from Google Play is
Intent Redirection
Your app(s) are vulnerable to Intent Redirection.
To address this issue, follow the steps in this Google Help Center article.
com.mydomain.somepackage.a->a
And the following is the simplified source code.
// MainActivity.java
public class MainActivity extends CordovaActivity
{
SpecialUtil specialUtil;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
specialUtil = new specialUtil(MainActivity.this);
}
#Override
public void onResume() {
super.onResume();
specialUtil.verifyServerIfNeeded(MainActivity.this);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == this.specialUtil.CERT_INVALID_POPUP_REQUEST_CODE) {
// the user clicked the return button in the alert dialog within WhiteScreen activity
this.specialUtil.declareAsFailure();
}
}
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
}
}
// com/mydomain/somepackage/SpecialUtil.java
public class SpecialUtil {
private SharedPreferences mSharedPreferences;
private SharedPreferences.Editor mSharedPreferencesEditor;
private SharedPreferences.OnSharedPreferenceChangeListener listener;
private Activity activity;
private boolean shownCertInvalidPopup = false;
public final int CERT_INVALID_POPUP_REQUEST_CODE = 1000;
public SpecialUtil(Activity activity) {
this.activity = activity;
this.mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(activity);
this.mSharedPreferencesEditor = mSharedPreferences.edit();
this.listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals("SOME_RESULT")) {
String result = mSharedPreferences.getString("SOME_RESULT", "");
if (result.equals("RESULT_OK")) {
SpecialUtil.this.declareAsSuccess();
} else if (result.equals("RESULT_CANCELED")) {
SpecialUtil.this.declareAsFailure();
}
}
}
};
this.mSharedPreferences.registerOnSharedPreferenceChangeListener(listener);
}
public void verifyServerIfNeeded(Activity activity) {
Intent intent = activity.getIntent();
if (this.isFlowA(intent)) {
this.removePermission(intent);
String url = intent.getStringExtra("url");
this.verifyServer(url);
} else if (this.isFlowB(intent)) {
this.removePermission(intent);
String payment_request_object_url = intent.getData().getQueryParameter("pay_req_obj");
String callback_url = intent.getData().getQueryParameter("callback");
this.verifyServer(payment_request_object_url);
}
}
public boolean isFlowA(Intent intent) {
if (intent.getAction().equals("someAction")) {
return true;
}
return false;
}
public boolean isFlowB(Intent intent) {
if (intent.getData() != null) {
String path = intent.getData().getPath();
if (path.equals("something")) {
return true;
}
}
return false;
}
public void verifyServer(final String httpsURL) {
new Thread(new Runnable() {
#Override
public void run() {
try {
boolean isCertValid = SpecialUtil.this.verify(httpsURL);
if (isCertValid) {
// do somthing
} else {
// show a white screen with an alert msg
SpecialUtil.this.activity.runOnUiThread(new Runnable() {
public void run() {
if (!shownCertInvalidPopup) {
shownCertInvalidPopup = true;
Intent intent = new Intent(SpecialUtil.this.activity, WhiteScreen.class);
SpecialUtil.this.activity.startActivityForResult(intent, CERT_INVALID_POPUP_REQUEST_CODE);
}
}
});
}
} catch (IOException e) {
SpecialUtil.this.declareAsFailure();
}
}
}).start();
}
private void declareAsSuccess() {
this.activity.setResult(Activity.RESULT_OK, SpecialUtil.this.activity.getIntent());
this.activity.finishAndRemoveTask();
}
public void declareAsFailure() {
this.activity.setResult(Activity.RESULT_CANCELED, this.activity.getIntent());
this.activity.finishAndRemoveTask();
}
private void removePermission(Intent intent) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
intent.removeFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.removeFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
}
}
// com/mydomain/somepackage/WhiteScreen.java
public class WhiteScreen extends Activity {
SpecialUtil specialUtil;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
specialUtil = new SpecialUtil(WhiteScreen.this);
String title = "someTitle";
final AlertDialog.Builder builder = new AlertDialog.Builder(WhiteScreen.this)
.setTitle(title)
.setPositiveButton(btn_text, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Don't start the process, quit App immediately
WhiteScreen.this.setResult(Activity.RESULT_CANCELED, WhiteScreen.this.getIntent());
WhiteScreen.this.finishAndRemoveTask();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}

UnAuthorized Overlay Error on entering Picture in Picture mode YoutubePlayer View Android?

I am doing live streaming for the youtube videos. By entering into the picture in picture mode the player pause the video with the Error UNAUTHORIZED_OVERLAY .
VideoLayout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linear_youtube_rootlayout"
android:orientation="vertical"
android:background="#color/black"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.youtube.player.YouTubePlayerView
android:id="#+id/youtube_player"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
ACtivity.cs
[Activity(Label = "", ResizeableActivity = true, Theme = "#style/Theme.MyAppTheme", TaskAffinity = "com.m", MainLauncher =true,AllowTaskReparenting = true, AutoRemoveFromRecents = true, ExcludeFromRecents = true, LaunchMode = Android.Content.PM.LaunchMode.SingleTask, SupportsPictureInPicture = true/*, ConfigurationChanges = Android.Content.PM.ConfigChanges.ScreenSize | Android.Content.PM.ConfigChanges.SmallestScreenSize | Android.Content.PM.ConfigChanges.ScreenLayout | Android.Content.PM.ConfigChanges.Orientation*/)]
public class YoutubeActivity: YouTubeBaseActivity,IYouTubePlayerOnInitializedListener,View.IOnClickListener,IYouTubePlayerPlayerStateChangeListener,IYouTubePlayerPlaybackEventListener,IYouTubePlayerOnFullscreenListener
{
private YouTubePlayerView mYoutubePlayer;
private PictureInPictureParams.Builder pictureInPictureParamsBuilder =
new PictureInPictureParams.Builder();
private LinearLayout linear_rootlayout;
private TextView txtMinimizevideo,txtCloseVideo;
private IYouTubePlayer youtubevideo;
private bool isbackbuttonpress = false;
private RelativeLayout relative_youtubecontrols;
public void OnInitializationFailure(IYouTubePlayerProvider p0, YouTubeInitializationResult p1)
{
}
public void OnInitializationSuccess(IYouTubePlayerProvider provider, IYouTubePlayer player, bool p2)
{
this.youtubevideo = player;
// youtubevideo.SetPlayerStyle(YouTubePlayerPlayerStyle.Minimal);
youtubevideo.SetOnFullscreenListener(this);
youtubevideo.SetPlayerStateChangeListener(this);
youtubevideo.SetPlaybackEventListener(this);
youtubevideo.FullscreenControlFlags = YouTubePlayer.FullscreenFlagCustomLayout;
youtubevideo.LoadVideo("VideoKey");
}
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.youtube_player_layout);
mYoutubePlayer = FindViewById<YouTubePlayerView>(Resource.Id.youtube_player);
linear_rootlayout = FindViewById<LinearLayout>(Resource.Id.linear_youtube_rootlayout);
relative_youtubecontrols = FindViewById<RelativeLayout>(Resource.Id.rel_youtube_control);
mYoutubePlayer.Initialize("SerialKey", this);
}
public override void OnPictureInPictureModeChanged(bool isInPictureInPictureMode, Configuration newConfig)
{
base.OnPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
if (IsInPictureInPictureMode)
{
youtubevideo.Release();
Window.AddFlags(WindowManagerFlags.Fullscreen);
}
}
public override void OnBackPressed()
{
pictureInPictureMode();
}
protected override void OnUserLeaveHint()
{
base.OnUserLeaveHint();
if (!IsInPictureInPictureMode)
{
pictureInPictureMode();
}
}
private void pictureInPictureMode()
{
isbackbuttonpress = true;
Rational aspectRatio = new Rational(200, 110);
pictureInPictureParamsBuilder.SetAspectRatio(aspectRatio).Build();
EnterPictureInPictureMode(pictureInPictureParamsBuilder.Build());
}
public void OnBuffering(bool p0)
{
}
public void OnPaused()
{
}
public void OnPlaying()
{
}
public void OnSeekTo(int p0)
{
}
public void OnStopped()
{
// youtubevideo.Play();
}
public void OnAdStarted()
{
}
public void OnError(YouTubePlayerErrorReason p0)
{
}
public void OnLoaded(string p0)
{
youtubevideo.Play();
}
public void OnLoading()
{
}
public void OnVideoEnded()
{
}
public void OnVideoStarted()
{
}
public void OnFullscreen(bool p0)
{
}
}
}
I am loading the video on the oninitializedsuccess and play the video in the on Loaded.
I have tried all the possible solutions so that no view is on the top of the youtube player view but it always gives me same error.
I think the issue is with your OnPictureInPictureModeChanged method..
public override void OnPictureInPictureModeChanged(bool isInPictureInPictureMode, Configuration newConfig)
{
base.OnPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
if (IsInPictureInPictureMode)
{
youtubevideo.Release();
Window.AddFlags(WindowManagerFlags.Fullscreen);
}
}
The reason I see here why the error UNAUTHORIZED_OVERLAY pops up is your AddFlags Call when in PIP Mode..
Modify your code as below and check to see if the error persists..
#Override
public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode) {
if (isInPictureInPictureMode) {
// Hide the controls in picture-in-picture mode.
...
} else {
// Restore the playback UI based on the playback status.
...
}
}
With regards to your addflags call, try the following code:
#Override
public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode) {
super.onPictureInPictureModeChanged(isInPictureInPictureMode);
if (!isInPictureInPictureMode) {
getApplication().startActivity(new Intent(this, getClass())
.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT));
}
}

Memory leak using Lollipop's transitions

I'm trying to implement transition between fragment (that attached to the MainActivity) with RecyclerView and the DetailActivity.
In my fragment, I've added RecyclerView listener in onStart() method:
#Override
public void onStart() {
super.onStart();
mRecyclerItemClickListener = new RecyclerItemClickListener(getActivity(), (view, position) -> {
Intent intent = new Intent(getActivity(), DetailActivity.class);
intent.putExtra(ConstantsManager.POSITION_ID_KEY, mFilmsAdapter.getImdbIdByPosition(position));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ActivityOptionsCompat optionsCompat = ActivityOptionsCompat
.makeSceneTransitionAnimation(getActivity());
ActivityCompat.startActivity(getActivity(), intent, optionsCompat.toBundle());
} else {
startActivity(intent);
}
});
mRecyclerView.addOnItemTouchListener(mRecyclerItemClickListener);
mSwipeRefreshLayout.setOnRefreshListener(this);
mTopFilmsPresenter.attachView(this);
mTopFilmsPresenter.getFilms();
}
You can see, that the transition begins in the if block. In the DetailActivity I have the following method which I call in onCreate():
#TargetApi(Build.VERSION_CODES.KITKAT)
private void setupWindowAnimations() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Explode explode = new Explode();
explode.setDuration(ConstantsManager.TRANSITION_DURATION);
getWindow().setEnterTransition(explode);
getWindow().setExitTransition(explode);
}
}
And in the MainActivity I have almost a similar method, which I also call in onCreate():
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void setWindowAnimations() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Explode explode = new Explode();
explode.setDuration(ConstantsManager.TRANSITION_DURATION);
getWindow().setEnterTransition(explode);
getWindow().setExitTransition(explode);
getWindow().setReenterTransition(explode);
getWindow().setReturnTransition(explode);
}
}
I've also implemented Transition.TransitionListener interface in the DetailActivity, because documentation says:
A transition listener receives notifications from a transition. Notifications indicate transition lifecycle events.
So I'm trying to remove listener in the onTransitionEnd(Transition transition), onTransitionCancel(Transition transition) and onTransitionPause(Transition transition) callbacks:
#Override
public void onTransitionStart(Transition transition) {
}
#TargetApi(Build.VERSION_CODES.KITKAT)
#Override
public void onTransitionEnd(Transition transition) {
transition.removeListener(this);
}
#TargetApi(Build.VERSION_CODES.KITKAT)
#Override
public void onTransitionCancel(Transition transition) {
transition.removeListener(this);
}
#TargetApi(Build.VERSION_CODES.KITKAT)
#Override
public void onTransitionPause(Transition transition) {
transition.removeListener(this);
}
#Override
public void onTransitionResume(Transition transition) {
}
I'm using LeakCanary for memory leaks detections and it detects a memory leak after transition:
So I am wondering how can I remove transition listener(s) to prevent this memory leak?
Ensure that you release the Activity reference since you are strongly holding onto it within the Transition class. A simple Transition.removeListener(this) (since your activity implements the interface) in it's onDestroy() method should prevent memory leaks.
Thanks Albert Vila for the answer. The problem lies in TransitionManager.sRunningTransitions according to this topic.

Android fragment lifecycle issue with actionbar

I want to realize the navigation of the fragments using the following code:
public abstract class BaseFragment extends Fragment {
private static String TAG = "BaseFragment";
private BaseFragmentActions baseFragmentActions;
#Override
public void onAttach(Context context) {
super.onAttach(context);
Activity activity = null;
if (context instanceof Activity){
activity = (Activity) context;
}
Log.i(TAG, "onAttach = ");
try {
baseFragmentActions = (BaseFragmentActions)activity;
} catch (ClassCastException e) {
}
Log.i("onAttach",""+(getBackStackCount()!=0));
baseFragmentActions.resetToolbarNavigation(getBackStackCount()!=0);
}
#Override
public void onDetach() {
super.onDetach();
Log.i("BaseFragment", "onDestroy = " + (getBackStackCount() - 1));
baseFragmentActions.resetToolbarNavigation((getBackStackCount() - 1) != 0);
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
private int getBackStackCount() {
int b = getActivity().getSupportFragmentManager().getBackStackEntryCount();
Log.i("getBackStackEntryCount", "====== "+b);
return b;
}
public interface BaseFragmentActions {
public void resetToolbarNavigation(boolean backNavigationEnabled);
}
}
All my fragments extend this Base Activity. And inside my main activity i implement BaseFragmentActions, and implemented this method:
#Override
public void resetToolbarNavigation(boolean backNavigationEnabled) {
Log.i("BaseActivity", "reset " + backNavigationEnabled);
getSupportActionBar().setDisplayHomeAsUpEnabled(backNavigationEnabled);
if (backNavigationEnabled) {
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.i("resetToolbarNavigation", "setNavigationOnClickListener");
onBackPressed();
}
});
} else {
initNavigation();
syncState();
}
}
Everything works fine but when I change the screen orientation we obtain error that getSupportActionBar = null.
This is because of what I call going to attach. How can I fix this error? I tried to make checking whether getSupportActionBar is not zero. I'm not getting an error, but "up" Arrow replaced hamburger...
Advise what you can do in this case. Also share links to navigate the implementation of such fragments. Sorry if something wrong written, or I made a grammatical error)).
Hi sorry for the delay in the answer, the problem you're having is because when onAttach is called the getSupportActionBar() is not set yet, instead you need to make sure the Activity is already created when interacting with Activity components, so just put your call inside the onActivityCreated method of your Fragment like this:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
baseFragmentActions.resetToolbarNavigation(getBackStackCount()!=0);
}

Updated Fragment view state lost when using setRetainInstance(true)

I am trying to save fragment state. I have an activity and several fragments. The sequence of actions: add first fragment, change view manually (make visibility of first LinearLayout GONE and second LinearLayout VISIBLE), detach fragment, add another one, detach it and again attach first fragment.
Adding/attaching/detaching works good but setRetainInstanse(true) saves only initial fragment state.
Finally I get first LinearLayout visible at my fragment (instead of second) so I've tried to make it by hands but it doesn't work:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) {
if (savedInstanceState.containsKey(BUNDLE_IS_LOADING)) {
if (savedInstanceState.getBoolean(BUNDLE_IS_LOADING)) {
mBlockContent.setVisibility(View.GONE);
mBlockProgress.setVisibility(View.VISIBLE);
} else {
mBlockContent.setVisibility(View.VISIBLE);
mBlockProgress.setVisibility(View.GONE);
}
}
}
}
setRetainInstance(true);
}
#Override
public void onSaveInstanceState(Bundle b) {
super.onSaveInstanceState(b);
b.putBoolean(BUNDLE_IS_LOADING,
mBlockProgress.getVisibility() == View.VISIBLE);
}
I use compatibility library rev. 11.
Solution for me:
private boolean isProgressing;
private void saveViewsState() {
isProgressing = mBlockProgress.getVisibility() == View.VISIBLE;
}
private void switchToProgress() {
mBlockContent.setVisibility(View.GONE);
mBlockProgress.setVisibility(View.VISIBLE);
}
private void switchToContent() {
mBlockContent.setVisibility(View.VISIBLE);
mBlockProgress.setVisibility(View.GONE);
}
#Override
public void onSaveInstanceState(Bundle b) {
super.onSaveInstanceState(b);
saveViewsState();
}
#Override
public void onPause() {
super.onPause();
saveViewsState();
}
#Override
public void onResume() {
super.onResume();
if (isProgressing) {
switchToProgress();
} else {
switchToContent();
}
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (isProgressing) {
switchToProgress();
} else {
switchToContent();
}
}

Categories

Resources