AndroidTV: Display Username

This patchs adds a registereduserName or RingId on the mainFragment.
To do this, we need to use a custom TitleView because the ring id is
too long and displayed badly withe the default titleview.

Change-Id: I052796ce5cdf37a284ba5cddf1b8cbd15f6ef65b
diff --git a/ring-android/app/src/main/AndroidManifest.xml b/ring-android/app/src/main/AndroidManifest.xml
index b57eab0..0071142 100644
--- a/ring-android/app/src/main/AndroidManifest.xml
+++ b/ring-android/app/src/main/AndroidManifest.xml
@@ -272,8 +272,8 @@
             android:icon="@drawable/ic_launcher"
             android:label="@string/title_activity_sflphone_home"
             android:logo="@drawable/ic_launcher"
-            android:theme="@style/AppThemeBase"
-            android:screenOrientation="landscape">
+            android:screenOrientation="landscape"
+            android:theme="@style/AppThemeBase">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
                 <category android:name="android.intent.category.LEANBACK_LAUNCHER" />
@@ -282,8 +282,8 @@
         <activity
             android:name=".tv.main.HomeActivity"
             android:label="@string/title_activity_sflphone_home"
-            android:theme="@style/Theme.Leanback"
-            android:screenOrientation="landscape"/>
+            android:screenOrientation="landscape"
+            android:theme="@style/Theme.Ring.Leanback.CustomTitle" />
         <activity
             android:name=".tv.account.TVAccountWizard"
             android:configChanges="screenSize|screenLayout|smallestScreenSize"
@@ -291,8 +291,9 @@
             android:screenOrientation="fullUser"
             android:theme="@style/AppThemeBase" />
 
-        <activity android:name="cx.ring.tv.search.SearchActivity"
-            android:theme="@style/Theme.Leanback"/>
+        <activity
+            android:name="cx.ring.tv.search.SearchActivity"
+            android:theme="@style/Theme.Leanback" />
 
         <activity
             android:name="cx.ring.tv.call.TVCallActivity"
diff --git a/ring-android/app/src/main/java/cx/ring/tv/main/MainFragment.java b/ring-android/app/src/main/java/cx/ring/tv/main/MainFragment.java
index 6e30cfe..6cde631 100644
--- a/ring-android/app/src/main/java/cx/ring/tv/main/MainFragment.java
+++ b/ring-android/app/src/main/java/cx/ring/tv/main/MainFragment.java
@@ -40,12 +40,11 @@
 
 public class MainFragment extends BaseBrowseFragment<MainPresenter> implements MainView {
     private static final String TAG = MainFragment.class.getSimpleName();
-
+    SpinnerFragment mSpinnerFragment;
     private ArrayObjectAdapter mRowsAdapter;
     private DisplayMetrics mMetrics;
     private BackgroundManager mBackgroundManager;
     private ArrayObjectAdapter cardRowAdapter;
-    SpinnerFragment mSpinnerFragment;
 
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
@@ -63,6 +62,7 @@
     public void onResume() {
         super.onResume();
         presenter.reloadConversations();
+        presenter.reloadAccountInfos();
     }
 
     private void setupUIElements() {
@@ -70,7 +70,6 @@
         mBackgroundManager.attach(getActivity().getWindow());
         mMetrics = new DisplayMetrics();
         getActivity().getWindowManager().getDefaultDisplay().getMetrics(mMetrics);
-        setBadgeDrawable(getActivity().getResources().getDrawable(R.drawable.ic_ring_logo_white));
         // over title
         setHeadersState(HEADERS_ENABLED);
         setHeadersTransitionOnBackEnabled(true);
@@ -136,6 +135,20 @@
         getActivity().startActivity(intent, null);
     }
 
+    @Override
+    public void displayAccountInfos(final String address) {
+        getActivity().runOnUiThread(new Runnable() {
+            @Override
+            public void run() {
+                if (address != null) {
+                    setTitle(address);
+                } else {
+                    setTitle("");
+                }
+            }
+        });
+    }
+
     private final class ItemViewClickedListener implements OnItemViewClickedListener {
         @Override
         public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item,
diff --git a/ring-android/app/src/main/java/cx/ring/tv/main/MainPresenter.java b/ring-android/app/src/main/java/cx/ring/tv/main/MainPresenter.java
index 2e4a65e..b7ff819 100644
--- a/ring-android/app/src/main/java/cx/ring/tv/main/MainPresenter.java
+++ b/ring-android/app/src/main/java/cx/ring/tv/main/MainPresenter.java
@@ -26,6 +26,7 @@
 import javax.inject.Named;
 
 import cx.ring.facades.ConversationFacade;
+import cx.ring.model.Account;
 import cx.ring.model.CallContact;
 import cx.ring.model.Conversation;
 import cx.ring.model.ServiceEvent;
@@ -93,6 +94,10 @@
             case CONVERSATIONS_CHANGED:
                 reloadConversations();
                 break;
+            case ACCOUNTS_CHANGED:
+            case NAME_REGISTRATION_ENDED:
+                reloadAccountInfos();
+                break;
         }
     }
 
@@ -124,4 +129,11 @@
         getView().callContact(accountID, ringID);
     }
 
+    public void reloadAccountInfos() {
+        String displayableAddress = null;
+        for (Account account : mAccountService.getAccounts()) {
+            displayableAddress = account.getDisplayUri();
+        }
+        getView().displayAccountInfos(displayableAddress);
+    }
 }
\ No newline at end of file
diff --git a/ring-android/app/src/main/java/cx/ring/tv/main/MainView.java b/ring-android/app/src/main/java/cx/ring/tv/main/MainView.java
index e007f4c..9b49062 100644
--- a/ring-android/app/src/main/java/cx/ring/tv/main/MainView.java
+++ b/ring-android/app/src/main/java/cx/ring/tv/main/MainView.java
@@ -30,4 +30,6 @@
     void showContacts(ArrayList<CallContact> contacts);
 
     void callContact(String accountID, String ringID);
+
+    void displayAccountInfos(String address);
 }
diff --git a/ring-android/app/src/main/java/cx/ring/tv/views/CustomTitleView.java b/ring-android/app/src/main/java/cx/ring/tv/views/CustomTitleView.java
new file mode 100644
index 0000000..35eff17
--- /dev/null
+++ b/ring-android/app/src/main/java/cx/ring/tv/views/CustomTitleView.java
@@ -0,0 +1,97 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
+ * in compliance with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
+ * or implied. See the License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package cx.ring.tv.views;
+
+import android.content.Context;
+import android.graphics.drawable.Drawable;
+import android.support.v17.leanback.widget.TitleViewAdapter;
+import android.util.AttributeSet;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.widget.RelativeLayout;
+import android.widget.TextView;
+
+import cx.ring.R;
+
+/**
+ * Custom title view to be used in {@link android.support.v17.leanback.app.BrowseFragment}.
+ */
+public class CustomTitleView extends RelativeLayout implements TitleViewAdapter.Provider {
+    private final TextView mTitleView;
+    private final View mLogoView;
+    private final View mSearchOrbView;
+
+    private final TitleViewAdapter mTitleViewAdapter = new TitleViewAdapter() {
+        @Override
+        public View getSearchAffordanceView() {
+            return mSearchOrbView;
+        }
+
+        @Override
+        public void setTitle(CharSequence titleText) {
+            CustomTitleView.this.setTitle(titleText);
+        }
+
+        @Override
+        public void setBadgeDrawable(Drawable drawable) {
+            //NOOP
+        }
+
+        @Override
+        public void setOnSearchClickedListener(OnClickListener listener) {
+            mSearchOrbView.setOnClickListener(listener);
+        }
+
+        @Override
+        public void updateComponentsVisibility(int flags) {
+            int visibility = (flags & SEARCH_VIEW_VISIBLE) == SEARCH_VIEW_VISIBLE
+                    ? View.VISIBLE : View.INVISIBLE;
+            mSearchOrbView.setVisibility(visibility);
+        }
+
+    };
+
+    public CustomTitleView(Context context) {
+        this(context, null);
+    }
+
+    public CustomTitleView(Context context, AttributeSet attrs) {
+        this(context, attrs, 0);
+    }
+
+    public CustomTitleView(Context context, AttributeSet attrs, int defStyle) {
+        super(context, attrs, defStyle);
+        View root = LayoutInflater.from(context).inflate(R.layout.tv_titleview, this);
+        mTitleView = (TextView) root.findViewById(R.id.title_text);
+        mLogoView = root.findViewById(R.id.title_badge);
+        mSearchOrbView = root.findViewById(R.id.title_orb);
+
+        setClipChildren(false);
+        setClipToPadding(false);
+    }
+
+    public void setTitle(CharSequence title) {
+        if (title != null) {
+            mTitleView.setText(title);
+            mTitleView.setVisibility(View.VISIBLE);
+            mLogoView.setVisibility(View.VISIBLE);
+        }
+    }
+
+    @Override
+    public TitleViewAdapter getTitleViewAdapter() {
+        return mTitleViewAdapter;
+    }
+}
\ No newline at end of file
diff --git a/ring-android/app/src/main/res/layout/titleview.xml b/ring-android/app/src/main/res/layout/titleview.xml
new file mode 100644
index 0000000..ff5662f
--- /dev/null
+++ b/ring-android/app/src/main/res/layout/titleview.xml
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="utf-8"?>
+<cx.ring.tv.views.CustomTitleView
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:id="@+id/browse_title_group"
+    android:padding="16dp"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content" />
\ No newline at end of file
diff --git a/ring-android/app/src/main/res/layout/tv_titleview.xml b/ring-android/app/src/main/res/layout/tv_titleview.xml
new file mode 100644
index 0000000..1172591
--- /dev/null
+++ b/ring-android/app/src/main/res/layout/tv_titleview.xml
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="utf-8"?>
+<merge xmlns:android="http://schemas.android.com/apk/res/android">
+
+    <android.support.v17.leanback.widget.SearchOrbView
+        android:id="@+id/title_orb"
+        android:layout_height="wrap_content"
+        android:layout_width="wrap_content"
+        android:transitionGroup="true"
+        android:layout_gravity="center_vertical|start"
+        android:layout_marginTop="8dp"
+        android:layout_marginStart="48dp" />
+
+    <TextView
+        android:id="@+id/title_text"
+        android:textAppearance="@android:style/TextAppearance.Large"
+        android:visibility="gone"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_marginEnd="24dp"
+        android:layout_centerVertical="true"
+        android:layout_toStartOf="@id/title_badge" />
+
+    <ImageView
+        android:id="@+id/title_badge"
+        android:layout_width="80dp"
+        android:layout_height="80dp"
+        android:padding="6dp"
+        android:src="@drawable/ic_ring_logo_white"
+        android:layout_alignParentEnd="true"
+        android:layout_gravity="center_vertical|end"
+        android:layout_marginEnd="24dp" />
+</merge>
\ No newline at end of file
diff --git a/ring-android/app/src/main/res/values/styles.xml b/ring-android/app/src/main/res/values/styles.xml
index 706cb34..2fc1874 100644
--- a/ring-android/app/src/main/res/values/styles.xml
+++ b/ring-android/app/src/main/res/values/styles.xml
@@ -100,4 +100,13 @@
         <item name="guidanceIconStyle">@style/Widget.Ring.Leanback.FirstStepGuidanceIconStyle</item>
         <item name="guidedStepBackground">@color/color_primary_light</item>
     </style>
+
+    <style name="Theme.Ring.LeanbackBrowse" parent="Theme.Leanback.Browse">
+        <item name="defaultSearchColor">@color/color_primary_light</item>
+    </style>
+
+    <style name="Theme.Ring.Leanback.CustomTitle" parent="Theme.Ring.LeanbackBrowse">
+        <item name="browseTitleViewLayout">@layout/titleview</item>
+    </style>
+
 </resources>
\ No newline at end of file