@Override public void onBackPressed() { Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.container); if (fragment != null) { if (fragment instanceof SearchFragment) { getTracker().track("onSearchCloseBackClick"); ((SearchFragment) fragment).closeSearch(); } else if (fragment instanceof PhoneFragment) { ((PhoneFragment) fragment).closePhone(); mAdsProvider.onPhoneClosed(this); } } else { mAdsProvider.onBackPressed(this); super.onBackPressed(); } }
private void showPhone(final String phoneNumber) { Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.container); if (fragment != null && fragment instanceof PhoneFragment) { ((PhoneFragment) fragment).updatePhone(phoneNumber); return; } if (isFabClicked) { return; } isFabClicked = true; mAdsProvider.onFabClick(this); mFloatingActionButton.hide(true); mFloatingActionButton.postDelayed( new Runnable() { @Override public void run() { PhoneFragment fragment = new PhoneFragment(); if (phoneNumber != null) { Bundle args = new Bundle(); args.putString(PhoneFragment.EXTRA_PHONE, phoneNumber); fragment.setArguments(args); } if (isFinishing()) { return; } getSupportFragmentManager() .beginTransaction() .addToBackStack(null) .add(R.id.container, fragment) .commit(); } }, 200l); mFloatingActionButton.postDelayed( new Runnable() { @Override public void run() { isFabClicked = false; } }, 500l); }
@Override protected void onCreate(Bundle savedInstanceState) { supportRequestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY); super.onCreate(savedInstanceState); GCMUtils.register(this); mGoogleApiClient = new GoogleApiClient.Builder(this) .addConnectionCallbacks( new GoogleApiClient.ConnectionCallbacks() { @Override public void onConnected(Bundle bundle) { Log.xd(MainActivity.this, "plus onConnected"); } @Override public void onConnectionSuspended(int i) { Log.xd(MainActivity.this, "plus onConnectionSuspended"); } }) .addOnConnectionFailedListener( new GoogleApiClient.OnConnectionFailedListener() { @Override public void onConnectionFailed(ConnectionResult connectionResult) { Log.xd(MainActivity.this, "plus onConnectionFailed " + connectionResult); if (!mIntentInProgress && !connectionResult.hasResolution()) { try { mIntentInProgress = true; PendingIntent resolution = connectionResult.getResolution(); if (resolution != null) { IntentSender intentSender = resolution.getIntentSender(); if (intentSender != null) { startIntentSenderForResult( intentSender, REQUEST_CODE_RESOLUTION, null, 0, 0, 0); } } } catch (IntentSender.SendIntentException e) { // The intent was canceled before it was sent. Return to the default // state and attempt to connect to get an updated ConnectionResult. mIntentInProgress = false; mGoogleApiClient.connect(); } } } }) .addApi(Plus.API) // .addScope(Plus.SCOPE_PLUS_LOGIN) .build(); FacebookSdk.sdkInitialize(getApplicationContext()); callbackManager = CallbackManager.Factory.create(); mAdsProvider.onCreate(this); setContentView(R.layout.activity_main); // Initialize MAT mobileAppTracker = MobileAppTracker.init( getApplicationContext(), BuildConfig.MAT_ADVERTISER_ID, BuildConfig.MAT_CONVERSION_KEY); getSupportFragmentManager() .addOnBackStackChangedListener( new FragmentManager.OnBackStackChangedListener() { @Override public void onBackStackChanged() { if (getSupportFragmentManager().getBackStackEntryCount() == 0) { if (!mFloatingActionButton.isVisible()) { mFloatingActionButton.show(true); } } } }); mDrawerInitializer.init(this, (ListView) findViewById(R.id.left_drawer)); findViewById(R.id.search_input) .setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { onSearchInputClick(v); } }); DrawerArrowDrawable drawable = new DrawerArrowDrawable(this, this); ImageView menuButton = (ImageView) findViewById(R.id.arrow); menuButton.setImageDrawable(drawable); menuButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { getTracker().track("onHomeHamburgerClick"); ((DrawerLayout) findViewById(R.id.drawer)).openDrawer(Gravity.START); } }); final View headerContainer = findViewById(R.id.header); final View floatHeaderContainer = findViewById(R.id.float_header); ViewTreeObserver viewTreeObserver = headerContainer.getViewTreeObserver(); viewTreeObserver.addOnGlobalLayoutListener( new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { ViewTreeObserver viewTreeObserver = headerContainer.getViewTreeObserver(); if (viewTreeObserver.isAlive()) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { viewTreeObserver.removeOnGlobalLayoutListener(this); } else { viewTreeObserver.removeGlobalOnLayoutListener(this); } } int height = headerContainer.getHeight(); if (height > 0) { mAdditionalAdapterHeight = height; } Log.xd(MainActivity.this, "height " + height); } }); final int defaultMargin = getResources().getDimensionPixelSize(R.dimen.default_margin_8); final int defaultHorizontalMargin = getResources().getDimensionPixelSize(R.dimen.default_margin_16); final int defaultMarginSmall = getResources().getDimensionPixelSize(R.dimen.default_margin_4); mFloatingActionButton = (FloatingActionButton) findViewById(R.id.fab); mViewPager = (ViewPager) findViewById(R.id.viewpager); mViewPager.setOffscreenPageLimit(3); mViewPager.setAdapter( new FragmentStatePagerAdapter(getSupportFragmentManager()) { @Override public Fragment getItem(int position) { switch (position) { case 0: return new SmartFragment(); case 1: return new RecentFragment(); case 2: return new ContactsFragment(); } throw new IllegalStateException("check fragment count"); } @Override public CharSequence getPageTitle(int position) { switch (position) { case 0: return getString(R.string.tab_speed_dial); case 1: return getString(R.string.tab_recents); case 2: return getString(R.string.tab_all_contacts); } return null; } @Override public int getCount() { return 3; } }); mSlidingTabLayout = (SlidingTabLayout) findViewById(R.id.sliding_tabs); mSlidingTabLayout.setViewPager(mViewPager); mSlidingTabLayout.setOnPageChangeListener( new ViewPager.OnPageChangeListener() { private int prevValue = 0; private int maxValue = -1; private int fabMargin = -1; @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { if (position == 0) { if (fabMargin == -1) { int width = mFloatingActionButton.getWidth(); if (width > 0) { fabMargin = width + defaultHorizontalMargin * 2; maxValue = mViewPager.getWidth() - fabMargin; } } if (positionOffsetPixels > maxValue) { positionOffsetPixels = maxValue; } if (prevValue < positionOffsetPixels || prevValue > positionOffsetPixels) { int value = positionOffsetPixels / 2; mFloatingActionButton.animate().translationX(value).setDuration(0l).start(); } } prevValue = positionOffsetPixels; } @Override public void onPageSelected(int position) {} @Override public void onPageScrollStateChanged(int state) {} }); mFloatingActionButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { showPhone(null); getTracker().track("onShowPhone:floatButton"); } }); mFloatHeaderScrollListener = new RecyclerView.OnScrollListener() { private RecyclerView mCurrentView; private int maxHeight; private LinearLayout.LayoutParams mHeaderLayoutParams; @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); if (newState == 1) { if (mCurrentView != null) { return; } mCurrentView = recyclerView; maxHeight = -(defaultMarginSmall + floatHeaderContainer.getHeight() - defaultMargin); mHeaderLayoutParams = (LinearLayout.LayoutParams) floatHeaderContainer.getLayoutParams(); } else if (newState == 0) { mCurrentView = null; } } @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); if (recyclerView != mCurrentView) { return; } int newTopMargin = mHeaderLayoutParams.topMargin - dy; if (newTopMargin > 0) { newTopMargin = 0; } else { if (newTopMargin < maxHeight) { newTopMargin = maxHeight; } } if (mHeaderLayoutParams.topMargin == newTopMargin) { return; } mHeaderLayoutParams.topMargin = newTopMargin; floatHeaderContainer.setLayoutParams(mHeaderLayoutParams); for (RecyclerView view : mRecyclerViews) { if (view != recyclerView) { view.scrollBy(dx, dy); } } } }; proceedIntent(getIntent()); if (UiUtil.hasKitKat() && !UiUtil.hasL()) { View mainContainer = findViewById(R.id.main_container); mainContainer.setPadding(0, UiUtil.getStatusBarHeight(this), 0, 0); } }