Base implementation of WFC disclaimer UI

Test: manual - Check that no error occurred when changing the wifi
calling settings to turn on.
Test: auto - Passed WifiCallingSettingsForSubTest,
WifiCallingDisclaimerFragmentTest and DisclaimerItemListAdapterTest.
Bug: 67872298

Change-Id: I789f530d3e16baa6e56feaa4269f6696976f747e
diff --git a/src/com/android/settings/Settings.java b/src/com/android/settings/Settings.java
index fac4253..97ff613 100644
--- a/src/com/android/settings/Settings.java
+++ b/src/com/android/settings/Settings.java
@@ -159,6 +159,7 @@
     public static class WebViewAppPickerActivity extends SettingsActivity { /* empty */ }
     public static class AdvancedConnectedDeviceActivity extends SettingsActivity { /* empty */ }
     public static class BluetoothDeviceDetailActivity extends SettingsActivity { /* empty */ }
+    public static class WifiCallingDisclaimerActivity extends SettingsActivity { /* empty */ }
 
     // Top level categories for new IA
     public static class NetworkDashboardActivity extends SettingsActivity {}
diff --git a/src/com/android/settings/core/gateway/SettingsGateway.java b/src/com/android/settings/core/gateway/SettingsGateway.java
index 147d0be..ba64a80 100644
--- a/src/com/android/settings/core/gateway/SettingsGateway.java
+++ b/src/com/android/settings/core/gateway/SettingsGateway.java
@@ -135,6 +135,7 @@
 import com.android.settings.wifi.WifiAPITest;
 import com.android.settings.wifi.WifiInfo;
 import com.android.settings.wifi.WifiSettings;
+import com.android.settings.wifi.calling.WifiCallingDisclaimerFragment;
 import com.android.settings.wifi.calling.WifiCallingSettings;
 import com.android.settings.wifi.p2p.WifiP2pSettings;
 import com.android.settings.wifi.savedaccesspoints.SavedAccessPointsWifiSettings;
@@ -260,6 +261,7 @@
             ConnectedDeviceDashboardFragment.class.getName(),
             UsbDetailsFragment.class.getName(),
             AppAndNotificationDashboardFragment.class.getName(),
+            WifiCallingDisclaimerFragment.class.getName(),
             AccountDashboardFragment.class.getName(),
             EnterprisePrivacySettings.class.getName(),
             WebViewAppPicker.class.getName(),
diff --git a/src/com/android/settings/wifi/calling/DisclaimerItem.java b/src/com/android/settings/wifi/calling/DisclaimerItem.java
new file mode 100644
index 0000000..6fd8b70
--- /dev/null
+++ b/src/com/android/settings/wifi/calling/DisclaimerItem.java
@@ -0,0 +1,137 @@
+/*
+ * Copyright (C) 2018 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 com.android.settings.wifi.calling;
+
+import android.content.Context;
+import android.content.SharedPreferences;
+import android.os.PersistableBundle;
+import android.telephony.CarrierConfigManager;
+import android.util.Log;
+
+import com.android.internal.annotations.VisibleForTesting;
+
+/**
+ * Interface to control disclaimer item from {@link WifiCallingDisclaimerFragment}.
+ */
+@VisibleForTesting
+public abstract class DisclaimerItem {
+    private static final String SHARED_PREFERENCES_NAME = "wfc_disclaimer_prefs";
+
+    protected final Context mContext;
+    protected final int mSubId;
+    private final CarrierConfigManager mCarrierConfigManager;
+
+    DisclaimerItem(Context context, int subId) {
+        mContext = context;
+        mSubId = subId;
+        mCarrierConfigManager = mContext.getSystemService(CarrierConfigManager.class);
+    }
+
+    /**
+     * Called by the {@link WifiCallingDisclaimerFragment} when a user has clicked the agree button.
+     */
+    void onAgreed() {
+        setBooleanSharedPrefs(getPrefKey(), true);
+    }
+
+    /**
+     * Checks whether the disclaimer item need to be displayed or not.
+     *
+     * @return Returns {@code true} if disclaimer item need to be displayed,
+     * {@code false} if not displayed.
+     */
+    boolean shouldShow() {
+        if (getBooleanSharedPrefs(getPrefKey(), false)) {
+            logd("shouldShow: false due to a user has already agreed.");
+            return false;
+        }
+        logd("shouldShow: true");
+        return true;
+    }
+
+    /**
+     * Gets the configuration values for a particular sub id.
+     *
+     * @return The {@link PersistableBundle} instance containing the config value for a
+     * particular phone id, or default values.
+     */
+    protected PersistableBundle getCarrierConfig() {
+        PersistableBundle config = mCarrierConfigManager.getConfigForSubId(mSubId);
+        if (config != null) {
+            return config;
+        }
+        // Return static default defined in CarrierConfigManager.
+        return CarrierConfigManager.getDefaultConfig();
+    }
+
+    protected void logd(String msg) {
+        Log.d(getName(), "[" + mSubId +  "] " + msg);
+    }
+
+    /**
+     * Gets a title id for disclaimer item.
+     *
+     * @return Title id for disclaimer item.
+     */
+    protected abstract int getTitleId();
+
+    /**
+     * Gets a message id for disclaimer item.
+     *
+     * @return Message id for disclaimer item.
+     */
+    protected abstract int getMessageId();
+
+    /**
+     * Gets a name of disclaimer item.
+     *
+     * @return Name of disclaimer item.
+     */
+    protected abstract String getName();
+
+    /**
+     * Gets a preference key to keep user's consent.
+     *
+     * @return Preference key to keep user's consent.
+     */
+    protected abstract String getPrefKey();
+
+    /**
+     * Gets the boolean value from shared preferences.
+     *
+     * @param key The key for the preference item.
+     * @param defValue Value to return if this preference does not exist.
+     * @return The boolean value of corresponding key, or defValue.
+     */
+    private boolean getBooleanSharedPrefs(String key, boolean defValue) {
+        SharedPreferences prefs = mContext.getSharedPreferences(SHARED_PREFERENCES_NAME,
+                Context.MODE_PRIVATE);
+        return prefs.getBoolean(key + mSubId, defValue);
+    }
+
+    /**
+     * Sets the boolean value to shared preferences.
+     *
+     * @param key The key for the preference item.
+     * @param value The value to be set for shared preferences.
+     */
+    private void setBooleanSharedPrefs(String key, boolean value) {
+        SharedPreferences prefs = mContext.getSharedPreferences(SHARED_PREFERENCES_NAME,
+                Context.MODE_PRIVATE);
+        prefs.edit().putBoolean(key + mSubId, value).apply();
+    }
+}
diff --git a/src/com/android/settings/wifi/calling/DisclaimerItemFactory.java b/src/com/android/settings/wifi/calling/DisclaimerItemFactory.java
new file mode 100644
index 0000000..6d8dc8f
--- /dev/null
+++ b/src/com/android/settings/wifi/calling/DisclaimerItemFactory.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright (C) 2018 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 com.android.settings.wifi.calling;
+
+import android.content.Context;
+
+import com.android.internal.annotations.VisibleForTesting;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * Factory class to create disclaimer items list.
+ */
+@VisibleForTesting
+public final class DisclaimerItemFactory {
+
+    /**
+     * Creates disclaimer items list.
+     *
+     * @param context The application context
+     * @param subId The subscription id.
+     * @return The {@link DisclaimerItem} list instance, if there are no items, return empty list.
+     */
+    public static List<DisclaimerItem> create(Context context, int subId) {
+        List<DisclaimerItem> itemList = getDisclaimerItemList(context, subId);
+        Iterator itr = itemList.iterator();
+        while (itr.hasNext()) {
+            DisclaimerItem item = (DisclaimerItem) itr.next();
+            if (!item.shouldShow()) {
+                itr.remove();
+            }
+        }
+        return itemList;
+    }
+
+    private static List<DisclaimerItem> getDisclaimerItemList(Context context, int subId) {
+        List<DisclaimerItem> itemList = new ArrayList<DisclaimerItem>();
+
+        return itemList;
+    }
+}
diff --git a/src/com/android/settings/wifi/calling/DisclaimerItemListAdapter.java b/src/com/android/settings/wifi/calling/DisclaimerItemListAdapter.java
new file mode 100644
index 0000000..4b5d19c
--- /dev/null
+++ b/src/com/android/settings/wifi/calling/DisclaimerItemListAdapter.java
@@ -0,0 +1,78 @@
+/*
+ * Copyright (C) 2018 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 com.android.settings.wifi.calling;
+
+import android.content.Context;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.TextView;
+
+import androidx.recyclerview.widget.RecyclerView;
+
+import com.android.internal.annotations.VisibleForTesting;
+import com.android.settings.R;
+
+import java.util.List;
+
+/**
+ * Adapter for disclaimer items list.
+ */
+public class DisclaimerItemListAdapter extends
+        RecyclerView.Adapter<DisclaimerItemListAdapter.DisclaimerItemViewHolder> {
+
+    private List<DisclaimerItem> mDisclaimerItemList;
+
+    public DisclaimerItemListAdapter(List<DisclaimerItem> list) {
+        mDisclaimerItemList = list;
+    }
+
+    @Override
+    public DisclaimerItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
+        LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(
+                Context.LAYOUT_INFLATER_SERVICE);
+        View view = inflater.inflate(R.layout.wfc_simple_disclaimer_item, null, false);
+        return new DisclaimerItemViewHolder(view);
+    }
+
+    @Override
+    public void onBindViewHolder(DisclaimerItemViewHolder holder, int position) {
+        holder.titleView.setText(mDisclaimerItemList.get(position).getTitleId());
+        holder.descriptionView.setText(mDisclaimerItemList.get(position).getMessageId());
+    }
+
+    @Override
+    public int getItemCount() {
+        return mDisclaimerItemList.size();
+    }
+
+    public static class DisclaimerItemViewHolder extends RecyclerView.ViewHolder {
+        @VisibleForTesting
+        static final int ID_DISCLAIMER_ITEM_TITLE = R.id.disclaimer_title;
+        @VisibleForTesting
+        static final int ID_DISCLAIMER_ITEM_DESCRIPTION = R.id.disclaimer_desc;
+
+        public final TextView titleView;
+        public final TextView descriptionView;
+
+        public DisclaimerItemViewHolder(View itemView) {
+            super(itemView);
+            titleView = itemView.findViewById(ID_DISCLAIMER_ITEM_TITLE);
+            descriptionView = itemView.findViewById(ID_DISCLAIMER_ITEM_DESCRIPTION);
+        }
+    }
+}
diff --git a/src/com/android/settings/wifi/calling/WifiCallingDisclaimerFragment.java b/src/com/android/settings/wifi/calling/WifiCallingDisclaimerFragment.java
new file mode 100644
index 0000000..7763226
--- /dev/null
+++ b/src/com/android/settings/wifi/calling/WifiCallingDisclaimerFragment.java
@@ -0,0 +1,148 @@
+/*
+ * Copyright (C) 2018 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 com.android.settings.wifi.calling;
+
+import android.app.Activity;
+import android.os.Bundle;
+import android.telephony.SubscriptionManager;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.Button;
+
+import androidx.recyclerview.widget.DividerItemDecoration;
+import androidx.recyclerview.widget.LinearLayoutManager;
+import androidx.recyclerview.widget.RecyclerView;
+import androidx.recyclerview.widget.RecyclerView.OnScrollListener;
+
+import com.android.internal.annotations.VisibleForTesting;
+import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
+import com.android.settings.R;
+import com.android.settings.core.InstrumentedFragment;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Fragment for displaying disclaimers for WFC.
+ */
+public class WifiCallingDisclaimerFragment extends InstrumentedFragment
+        implements View.OnClickListener {
+    private static final String TAG = "WifiCallingDisclaimerFragment";
+
+    private static final String STATE_IS_SCROLL_TO_BOTTOM = "state_is_scroll_to_bottom";
+
+    private List<DisclaimerItem> mDisclaimerItemList = new ArrayList<DisclaimerItem>();
+    private Button mAgreeButton;
+    private Button mDisagreeButton;
+    private boolean mScrollToBottom;
+
+    @Override
+    public int getMetricsCategory() {
+        return MetricsEvent.WIFI_CALLING;
+    }
+
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        final Bundle args = getArguments();
+        final int subId = (args != null) ? args.getInt(WifiCallingSettingsForSub.EXTRA_SUB_ID)
+                : SubscriptionManager.DEFAULT_SUBSCRIPTION_ID;
+
+        mDisclaimerItemList = DisclaimerItemFactory.create(getActivity(), subId);
+        if (mDisclaimerItemList.isEmpty()) {
+            finish(Activity.RESULT_OK);
+            return;
+        }
+
+        if (savedInstanceState != null) {
+            mScrollToBottom = savedInstanceState.getBoolean(
+                    STATE_IS_SCROLL_TO_BOTTOM, mScrollToBottom);
+        }
+    }
+
+    @Override
+    public View onCreateView(LayoutInflater inflater, ViewGroup container,
+            Bundle savedInstanceState) {
+
+        final View view = inflater.inflate(R.layout.wfc_disclaimer_fragment, container, false);
+
+        mAgreeButton = view.findViewById(R.id.agree_button);
+        mAgreeButton.setOnClickListener(this);
+        mDisagreeButton = view.findViewById(R.id.disagree_button);
+        mDisagreeButton.setOnClickListener(this);
+
+        final RecyclerView recyclerView = (RecyclerView) view.findViewById(
+                R.id.disclaimer_item_list);
+        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
+        recyclerView.setAdapter(new DisclaimerItemListAdapter(mDisclaimerItemList));
+
+        RecyclerView.ItemDecoration itemDecoration =
+                new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL);
+        recyclerView.addItemDecoration(itemDecoration);
+
+        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
+            @Override
+            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
+                super.onScrolled(recyclerView, dx, dy);
+                if (!recyclerView.canScrollVertically(1 /* scrolling down */)) {
+                    mScrollToBottom = true;
+                    updateButtonState();
+                    recyclerView.removeOnScrollListener(this);
+                }
+            }
+        });
+
+        return view;
+    }
+
+    @Override
+    public void onResume() {
+        super.onResume();
+        updateButtonState();
+    }
+
+    @Override
+    public void onSaveInstanceState(Bundle outState) {
+        super.onSaveInstanceState(outState);
+        outState.putBoolean(STATE_IS_SCROLL_TO_BOTTOM, mScrollToBottom);
+    }
+
+    private void updateButtonState() {
+        mAgreeButton.setEnabled(mScrollToBottom);
+    }
+
+    @Override
+    public void onClick(View v) {
+        if (v == mAgreeButton) {
+            for (DisclaimerItem item : mDisclaimerItemList) {
+                item.onAgreed();
+            }
+            finish(Activity.RESULT_OK);
+        } else if (v == mDisagreeButton) {
+            finish(Activity.RESULT_CANCELED);
+        }
+    }
+
+    @VisibleForTesting
+    void finish(int result) {
+        Activity activity = getActivity();
+        activity.setResult(result, null);
+        activity.finish();
+    }
+}
\ No newline at end of file
diff --git a/src/com/android/settings/wifi/calling/WifiCallingSettingsForSub.java b/src/com/android/settings/wifi/calling/WifiCallingSettingsForSub.java
index 625de38..525b59b 100644
--- a/src/com/android/settings/wifi/calling/WifiCallingSettingsForSub.java
+++ b/src/com/android/settings/wifi/calling/WifiCallingSettingsForSub.java
@@ -53,6 +53,7 @@
 import com.android.settings.SettingsActivity;
 import com.android.settings.SettingsPreferenceFragment;
 import com.android.settings.Utils;
+import com.android.settings.core.SubSettingLauncher;
 import com.android.settings.widget.SwitchBar;
 
 /**
@@ -69,9 +70,13 @@
     private static final String BUTTON_WFC_ROAMING_MODE = "wifi_calling_roaming_mode";
     private static final String PREFERENCE_EMERGENCY_ADDRESS = "emergency_address_key";
 
-    private static final int REQUEST_CHECK_WFC_EMERGENCY_ADDRESS = 1;
+    @VisibleForTesting
+    static final int REQUEST_CHECK_WFC_EMERGENCY_ADDRESS = 1;
+    @VisibleForTesting
+    static final int REQUEST_CHECK_WFC_DISCLAIMER = 2;
 
     public static final String EXTRA_LAUNCH_CARRIER_APP = "EXTRA_LAUNCH_CARRIER_APP";
+    public static final String EXTRA_SUB_ID = "EXTRA_SUB_ID";
 
     protected static final String FRAGMENT_BUNDLE_SUBID = "subId";
 
@@ -172,7 +177,7 @@
 
         mEmptyView = getView().findViewById(android.R.id.empty);
         setEmptyView(mEmptyView);
-        final Resources res = SubscriptionManager.getResourcesForSubId(getContext(), mSubId);
+        final Resources res = getResourcesForSubId();
         String emptyViewText = res.getString(R.string.wifi_calling_off_explanation,
                 res.getString(R.string.wifi_calling_off_explanation_2));
         mEmptyView.setText(emptyViewText);
@@ -416,14 +421,17 @@
             return;
         }
 
-        // Call address management activity before turning on WFC
-        Intent carrierAppIntent = getCarrierActivityIntent();
-        if (carrierAppIntent != null) {
-            carrierAppIntent.putExtra(EXTRA_LAUNCH_CARRIER_APP, LAUCH_APP_ACTIVATE);
-            startActivityForResult(carrierAppIntent, REQUEST_CHECK_WFC_EMERGENCY_ADDRESS);
-        } else {
-            updateWfcMode(true);
-        }
+        // Launch disclaimer fragment before turning on WFC
+        final Context context = getActivity();
+        final Bundle args = new Bundle();
+        args.putInt(EXTRA_SUB_ID, mSubId);
+        new SubSettingLauncher(context)
+                .setDestination(WifiCallingDisclaimerFragment.class.getName())
+                .setArguments(args)
+                .setTitleRes(R.string.wifi_calling_settings_title)
+                .setSourceMetricsCategory(getMetricsCategory())
+                .setResultListener(this, REQUEST_CHECK_WFC_DISCLAIMER)
+                .launch();
     }
 
     /*
@@ -476,12 +484,30 @@
 
         final Context context = getActivity();
 
-        if (requestCode == REQUEST_CHECK_WFC_EMERGENCY_ADDRESS) {
-            Log.d(TAG, "WFC emergency address activity result = " + resultCode);
+        Log.d(TAG, "WFC activity request = " + requestCode + " result = " + resultCode);
 
-            if (resultCode == Activity.RESULT_OK) {
-                updateWfcMode(true);
-            }
+        switch (requestCode) {
+            case REQUEST_CHECK_WFC_EMERGENCY_ADDRESS:
+                if (resultCode == Activity.RESULT_OK) {
+                    updateWfcMode(true);
+                }
+                break;
+            case REQUEST_CHECK_WFC_DISCLAIMER:
+                if (resultCode == Activity.RESULT_OK) {
+                    // Call address management activity before turning on WFC
+                    Intent carrierAppIntent = getCarrierActivityIntent();
+                    if (carrierAppIntent != null) {
+                        carrierAppIntent.putExtra(EXTRA_LAUNCH_CARRIER_APP, LAUCH_APP_ACTIVATE);
+                        startActivityForResult(carrierAppIntent,
+                                REQUEST_CHECK_WFC_EMERGENCY_ADDRESS);
+                    } else {
+                        updateWfcMode(true);
+                    }
+                }
+                break;
+            default:
+                Log.e(TAG, "Unexpected request: " + requestCode);
+                break;
         }
     }
 
@@ -568,4 +594,9 @@
         }
         return resId;
     }
+
+    @VisibleForTesting
+    Resources getResourcesForSubId() {
+        return SubscriptionManager.getResourcesForSubId(getContext(), mSubId, false);
+    }
 }