From fca619f7165a20ada6b372b1aca7eebd5bf67512 Mon Sep 17 00:00:00 2001
From: Amirhossein <amirhossein.naghshzan@savoirfairelinux.com>
Date: Wed, 18 Dec 2019 05:49:12 -0500
Subject: [PATCH] accountSummary: add bottomSheet behavior to new device layout

Change-Id: I78ea4cb561c89bb5d59c0fe2a62c5f4753f0044b
---
 .../ring/account/AccountEditionActivity.java  | 17 +++++
 .../account/RingAccountSummaryFragment.java   | 52 +++++++++-----
 .../ring/interfaces/BackHandlerInterface.java |  2 +-
 .../cx/ring/views/LinkNewDeviceLayout.java    | 68 -------------------
 .../main/res/layout/add_new_device_layout.xml | 38 +++++++----
 5 files changed, 76 insertions(+), 101 deletions(-)
 delete mode 100644 ring-android/app/src/main/java/cx/ring/views/LinkNewDeviceLayout.java

diff --git a/ring-android/app/src/main/java/cx/ring/account/AccountEditionActivity.java b/ring-android/app/src/main/java/cx/ring/account/AccountEditionActivity.java
index 05531c22a..5a94750ba 100644
--- a/ring-android/app/src/main/java/cx/ring/account/AccountEditionActivity.java
+++ b/ring-android/app/src/main/java/cx/ring/account/AccountEditionActivity.java
@@ -57,6 +57,7 @@ import cx.ring.fragments.AdvancedAccountFragment;
 import cx.ring.fragments.GeneralAccountFragment;
 import cx.ring.fragments.MediaPreferenceFragment;
 import cx.ring.fragments.SecurityAccountFragment;
+import cx.ring.interfaces.BackHandlerInterface;
 
 public class AccountEditionActivity extends AppCompatActivity implements AccountEditionView {
 
@@ -80,6 +81,8 @@ public class AccountEditionActivity extends AppCompatActivity implements Account
     private MenuItem mItemAdvanced;
     private MenuItem mItemBlacklist;
 
+    private BackHandlerInterface mBackHandlerInterface;
+
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
@@ -176,6 +179,9 @@ public class AccountEditionActivity extends AppCompatActivity implements Account
 
     @Override
     public void onBackPressed() {
+        if (mBackHandlerInterface.onBackPressed()) {
+            return;
+        }
         if (frameLayout.getVisibility() == View.VISIBLE) {
             super.onBackPressed();
         } else {
@@ -185,6 +191,12 @@ public class AccountEditionActivity extends AppCompatActivity implements Account
         }
     }
 
+    @Override
+    protected void onDestroy() {
+        super.onDestroy();
+        mBackHandlerInterface = null;
+    }
+
     @Override
     public boolean onOptionsItemSelected(MenuItem item) {
         switch (item.getItemId()) {
@@ -339,4 +351,9 @@ public class AccountEditionActivity extends AppCompatActivity implements Account
             return result;
         }
     }
+
+    public void setOnBackPressedListener(BackHandlerInterface backPressedListener) {
+        mBackHandlerInterface = backPressedListener;
+    }
+
 }
diff --git a/ring-android/app/src/main/java/cx/ring/account/RingAccountSummaryFragment.java b/ring-android/app/src/main/java/cx/ring/account/RingAccountSummaryFragment.java
index 1d30e7e15..3a7216290 100644
--- a/ring-android/app/src/main/java/cx/ring/account/RingAccountSummaryFragment.java
+++ b/ring-android/app/src/main/java/cx/ring/account/RingAccountSummaryFragment.java
@@ -28,6 +28,8 @@ import android.graphics.Typeface;
 import android.net.Uri;
 import android.os.Bundle;
 
+import com.google.android.material.bottomsheet.BottomSheetBehavior;
+import com.google.android.material.card.MaterialCardView;
 import com.google.android.material.chip.Chip;
 import com.google.android.material.dialog.MaterialAlertDialogBuilder;
 import com.google.android.material.snackbar.Snackbar;
@@ -69,7 +71,6 @@ import cx.ring.mvp.BaseSupportFragment;
 import cx.ring.services.AccountService;
 import cx.ring.utils.AndroidFileUtils;
 import cx.ring.utils.KeyboardVisibilityManager;
-import cx.ring.views.LinkNewDeviceLayout;
 import io.reactivex.android.schedulers.AndroidSchedulers;
 
 public class RingAccountSummaryFragment extends BaseSupportFragment<RingAccountSummaryPresenter> implements BackHandlerInterface,
@@ -89,8 +90,6 @@ public class RingAccountSummaryFragment extends BaseSupportFragment<RingAccountS
     /*
     UI Bindings
      */
-    @BindView(R.id.linkaccount_container)
-    LinkNewDeviceLayout mLinkAccountView;
 
     @BindView(R.id.ring_password)
     EditText mRingPassword;
@@ -117,7 +116,7 @@ public class RingAccountSummaryFragment extends BaseSupportFragment<RingAccountS
     Button mChangePasswordBtn;
 
     @BindView(R.id.layout_add_device)
-    LinearLayout mAddAccountLayout;
+    MaterialCardView mAddAccountLayout;
 
     @BindView(R.id.export_account_btn)
     Button mExportButton;
@@ -159,6 +158,7 @@ public class RingAccountSummaryFragment extends BaseSupportFragment<RingAccountS
     private String mBestName = "";
     private String mAccountId = "";
     private File mCacheArchive = null;
+    private BottomSheetBehavior mSheetBehavior;
 
     @Inject
     AccountService mAccountService;
@@ -166,7 +166,8 @@ public class RingAccountSummaryFragment extends BaseSupportFragment<RingAccountS
     @Override
     public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
         super.onViewCreated(view, savedInstanceState);
-        mLinkAccountView.setContainer(this);
+        mSheetBehavior = BottomSheetBehavior.from(mAddAccountLayout);
+
         hidePopWizard();
         if (getArguments() != null) {
             String accountId = getArguments().getString(AccountEditionActivity.ACCOUNT_ID_KEY);
@@ -174,6 +175,25 @@ public class RingAccountSummaryFragment extends BaseSupportFragment<RingAccountS
                 presenter.setAccountId(accountId);
             }
         }
+
+        ((AccountEditionActivity) getActivity()).setOnBackPressedListener(this);
+
+        mSheetBehavior.addBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
+            @Override
+            public void onStateChanged(@NonNull View view, int i) {
+                if (mSheetBehavior.getState() == BottomSheetBehavior.STATE_COLLAPSED){
+                    mPasswordLayout.setVisibility(mAccountHasPassword ? View.VISIBLE : View.GONE);
+                    mEndBtn.setVisibility(View.GONE);
+                    mStartBtn.setVisibility(View.VISIBLE);
+                    mExportInfos.setText(R.string.account_link_export_info);
+                }
+            }
+
+            @Override
+            public void onSlide(@NonNull View view, float v) {
+
+            }
+        });
     }
 
     public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
@@ -277,10 +297,13 @@ public class RingAccountSummaryFragment extends BaseSupportFragment<RingAccountS
     BackHandlerInterface
     */
     @Override
-    public void onBackPressed() {
+    public boolean onBackPressed() {
         if (isDisplayingWizard()) {
             hideWizard();
+            return true;
         }
+
+        return false;
     }
 
     /*
@@ -297,22 +320,17 @@ public class RingAccountSummaryFragment extends BaseSupportFragment<RingAccountS
     }
 
     private void showWizard() {
-        mLinkAccountView.setVisibility(View.VISIBLE);
-        mPasswordLayout.setVisibility(mAccountHasPassword ? View.VISIBLE : View.GONE);
-        mEndBtn.setVisibility(View.GONE);
-        mStartBtn.setVisibility(View.VISIBLE);
-        mExportInfos.setText(R.string.account_link_export_info);
+        mSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
     }
 
     @OnClick(R.id.btn_end_export)
     @SuppressWarnings("unused")
     public void hidePopWizard() {
-        mLinkAccountView.setVisibility(View.GONE);
+        mSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
     }
 
     public void hideWizard() {
-        mLinkAccountView.setVisibility(View.GONE);
-        mRingPassword.setText("");
+        mSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
         KeyboardVisibilityManager.hideKeyboard(getActivity(), 0);
     }
 
@@ -344,7 +362,7 @@ public class RingAccountSummaryFragment extends BaseSupportFragment<RingAccountS
     }
 
     public boolean isDisplayingWizard() {
-        return mLinkAccountView.getVisibility() == View.VISIBLE;
+        return mSheetBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED;
     }
 
     @OnEditorAction(R.id.ring_password)
@@ -439,8 +457,8 @@ public class RingAccountSummaryFragment extends BaseSupportFragment<RingAccountS
 
     @Override
     public void showPIN(final String pin) {
-        hideWizard();
-        mLinkAccountView.setVisibility(View.VISIBLE);
+        mRingPassword.setText("");
+        mSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
         mPasswordLayout.setVisibility(View.GONE);
         mEndBtn.setVisibility(View.VISIBLE);
         mStartBtn.setVisibility(View.GONE);
diff --git a/ring-android/app/src/main/java/cx/ring/interfaces/BackHandlerInterface.java b/ring-android/app/src/main/java/cx/ring/interfaces/BackHandlerInterface.java
index ae8b3765b..55043af61 100644
--- a/ring-android/app/src/main/java/cx/ring/interfaces/BackHandlerInterface.java
+++ b/ring-android/app/src/main/java/cx/ring/interfaces/BackHandlerInterface.java
@@ -19,5 +19,5 @@
 package cx.ring.interfaces;
 
 public interface BackHandlerInterface {
-    void onBackPressed();
+    boolean onBackPressed();
 }
diff --git a/ring-android/app/src/main/java/cx/ring/views/LinkNewDeviceLayout.java b/ring-android/app/src/main/java/cx/ring/views/LinkNewDeviceLayout.java
deleted file mode 100644
index 08b94034d..000000000
--- a/ring-android/app/src/main/java/cx/ring/views/LinkNewDeviceLayout.java
+++ /dev/null
@@ -1,68 +0,0 @@
-/*
- *  Copyright (C) 2004-2019 Savoir-faire Linux Inc.
- *
- *  Author: Loïc Siret <loic.siret@savoirfairelinux.com>
- *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 3 of the License, or
- *  (at your option) any later version.
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-package cx.ring.views;
-
-import android.content.Context;
-import android.util.AttributeSet;
-import android.view.KeyEvent;
-import android.widget.LinearLayout;
-
-import cx.ring.account.RingAccountSummaryFragment;
-
-public class LinkNewDeviceLayout extends LinearLayout {
-
-    private RingAccountSummaryFragment container = null;
-
-    public LinkNewDeviceLayout(Context context, AttributeSet attrs) {
-        super(context, attrs);
-    }
-
-    public LinkNewDeviceLayout(Context context) {
-        super(context);
-    }
-
-    /**
-     * Overrides the handling of the back key to move back to
-     * dismiss the view, instead of only dismissing the input method.
-     */
-    @Override
-    public boolean dispatchKeyEventPreIme(KeyEvent event) {
-        if (container != null &&
-                event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
-            KeyEvent.DispatcherState state = getKeyDispatcherState();
-            if (state != null) {
-                if (event.getAction() == KeyEvent.ACTION_DOWN
-                        && event.getRepeatCount() == 0) {
-                    state.startTracking(event, this);
-                    return true;
-                } else if (event.getAction() == KeyEvent.ACTION_UP
-                        && !event.isCanceled() && state.isTracking(event)) {
-                    container.onBackPressed();
-                    return true;
-                }
-            }
-        }
-
-        return super.dispatchKeyEventPreIme(event);
-    }
-
-    public void setContainer(RingAccountSummaryFragment container) {
-        this.container = container;
-    }
-}
diff --git a/ring-android/app/src/main/res/layout/add_new_device_layout.xml b/ring-android/app/src/main/res/layout/add_new_device_layout.xml
index a17e96156..25e3db67b 100644
--- a/ring-android/app/src/main/res/layout/add_new_device_layout.xml
+++ b/ring-android/app/src/main/res/layout/add_new_device_layout.xml
@@ -16,23 +16,33 @@ GNU General Public License for more details.
 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <https://www.gnu.org/licenses/>.
 -->
-<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
+<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_gravity="bottom"
     android:clipToPadding="false"
-    android:paddingTop="20dp"
     tools:showIn="@layout/frag_acc_summary">
 
-    <LinearLayout
+    <com.google.android.material.card.MaterialCardView
         android:id="@+id/layout_add_device"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
-        android:background="@color/colorPrimary"
+        app:cardCornerRadius="15dp"
+        android:layout_marginBottom="-15dp"
         android:elevation="4dp"
-        android:orientation="vertical">
+        app:behavior_hideable="false"
+        app:behavior_peekHeight="50dp"
+        app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
+        >
+
+        <LinearLayout
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:background="@color/colorPrimary"
+            android:paddingBottom="15dp"
+            android:orientation="vertical">
 
         <com.google.android.material.button.MaterialButton
             android:id="@+id/btn_add_device"
@@ -43,13 +53,7 @@ along with this program.  If not, see <https://www.gnu.org/licenses/>.
             android:text="@string/account_link_export_button"
             android:textColor="@color/colorOnPrimary" />
 
-        <cx.ring.views.LinkNewDeviceLayout
-            android:id="@+id/linkaccount_container"
-            android:layout_width="match_parent"
-            android:layout_height="wrap_content"
-            android:orientation="vertical">
-
-            <ScrollView
+        <ScrollView
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content">
 
@@ -80,6 +84,7 @@ along with this program.  If not, see <https://www.gnu.org/licenses/>.
                         app:passwordToggleTint="@color/colorPrimary"
                         android:paddingLeft="16dp"
                         android:paddingRight="16dp"
+                        android:layout_marginBottom="8dp"
                         app:passwordToggleEnabled="true">
 
                         <com.google.android.material.textfield.TextInputEditText
@@ -99,6 +104,7 @@ along with this program.  If not, see <https://www.gnu.org/licenses/>.
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         android:layout_gravity="center_horizontal"
+                        android:layout_marginBottom="8dp"
                         android:text="@string/account_start_export_button"
                         app:backgroundTint="@color/colorSecondary"
                         android:textColor="@color/colorOnSecondary"/>
@@ -116,6 +122,8 @@ along with this program.  If not, see <https://www.gnu.org/licenses/>.
                 </LinearLayout>
             </ScrollView>
 
-        </cx.ring.views.LinkNewDeviceLayout>
-    </LinearLayout>
-</FrameLayout>
\ No newline at end of file
+        </LinearLayout>
+
+        </com.google.android.material.card.MaterialCardView>
+
+</androidx.coordinatorlayout.widget.CoordinatorLayout>
\ No newline at end of file
-- 
GitLab