1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.example.android.autofill.app.edgecases;
17 
18 import android.content.Intent;
19 import android.os.Bundle;
20 import android.support.v7.app.AppCompatActivity;
21 import android.view.View;
22 import android.view.autofill.AutofillManager;
23 import android.widget.Toast;
24 
25 import com.example.android.autofill.app.R;
26 import com.example.android.autofill.app.Util;
27 import com.example.android.autofill.app.view.autofillable.CustomVirtualView;
28 import com.example.android.autofill.app.view.autofillable.ScrollableCustomVirtualView;
29 
30 /**
31  * Activity used to demonstrated safe partitioning of data.
32  * <p>
33  * <p>It has multiple partitions, but only accepts autofill on each partition at time.
34  */
35 /*
36  * TODO list
37  *
38  * - Fix top margin.
39  * - Use a combo box to select if credit card expiration date is expressed as date or text.
40  * - Use a dedicated TextView (instead of Toast) for error messages.
41  * - Use wrap_context to CustomView container.
42  * - Use different background color (or borders) for each partition.
43  * - Add more partitions (like address) - should match same partitions from service.
44  * - Add more hints (like w3c ones) - should match same hints from service.
45  */
46 public class MultiplePartitionsActivity extends AppCompatActivity {
47 
48     private ScrollableCustomVirtualView mCustomVirtualView;
49     private AutofillManager mAutofillManager;
50     private CustomVirtualView.Partition mCredentialsPartition;
51     private CustomVirtualView.Partition mCcPartition;
52 
53     @Override
onCreate(Bundle savedInstanceState)54     protected void onCreate(Bundle savedInstanceState) {
55         super.onCreate(savedInstanceState);
56         setContentView(R.layout.multiple_partitions_activity);
57         mCustomVirtualView = findViewById(R.id.custom_view);
58         mCredentialsPartition =
59                 mCustomVirtualView.addPartition(getString(R.string.partition_credentials));
60         mCredentialsPartition.addLine("username", View.AUTOFILL_TYPE_TEXT,
61                 getString(R.string.username_label),
62                 "         ", false, View.AUTOFILL_HINT_USERNAME);
63         mCredentialsPartition.addLine("password", View.AUTOFILL_TYPE_TEXT,
64                 getString(R.string.password_label),
65                 "         ", true, View.AUTOFILL_HINT_PASSWORD);
66         int ccExpirationType = View.AUTOFILL_TYPE_DATE;
67         // TODO: add a checkbox to switch between text / date instead
68         Intent intent = getIntent();
69         if (intent != null) {
70             int newType = intent.getIntExtra("dateType", -1);
71             if (newType != -1) {
72                 ccExpirationType = newType;
73                 String typeMessage = getString(R.string.message_credit_card_expiration_type,
74                         Util.getAutofillTypeAsString(ccExpirationType));
75                 // TODO: display type in a header or proper status widget
76                 Toast.makeText(getApplicationContext(), typeMessage, Toast.LENGTH_LONG).show();
77             }
78         }
79         mCcPartition = mCustomVirtualView.addPartition(getString(R.string.partition_credit_card));
80         mCcPartition.addLine("ccNumber", View.AUTOFILL_TYPE_TEXT,
81                 getString(R.string.credit_card_number_label),
82                 "         ", true, View.AUTOFILL_HINT_CREDIT_CARD_NUMBER);
83         mCcPartition.addLine("ccDay", View.AUTOFILL_TYPE_TEXT,
84                 getString(R.string.credit_card_expiration_day_label),
85                 "         ", true, View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DAY);
86         mCcPartition.addLine("ccMonth", ccExpirationType,
87                 getString(R.string.credit_card_expiration_month_label),
88                 "         ", true, View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_MONTH);
89         mCcPartition.addLine("ccYear", View.AUTOFILL_TYPE_TEXT,
90                 getString(R.string.credit_card_expiration_year_label),
91                 "         ", true, View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_YEAR);
92         mCcPartition.addLine("ccDate", ccExpirationType,
93                 getString(R.string.credit_card_expiration_date_label),
94                 "         ", true, View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DATE);
95         mCcPartition.addLine("ccSecurityCode", View.AUTOFILL_TYPE_TEXT,
96                 getString(R.string.credit_card_security_code_label),
97                 "         ", true, View.AUTOFILL_HINT_CREDIT_CARD_SECURITY_CODE);
98         mAutofillManager = getSystemService(AutofillManager.class);
99         findViewById(R.id.clear).setOnClickListener(new View.OnClickListener() {
100             @Override
101             public void onClick(View view) {
102                 resetFields();
103                 mCustomVirtualView.resetPositions();
104                 mAutofillManager.cancel();
105             }
106         });
107     }
108 
resetFields()109     private void resetFields() {
110         mCredentialsPartition.reset();
111         mCcPartition.reset();
112         mCustomVirtualView.postInvalidate();
113     }
114 }
115