blob: 06a425638854f3811f9a912849e2c5e3dfba7830 [file] [log] [blame]
Ben Murdochaf554522010-09-10 22:09:30 +01001/*
2 * Copyright (C) 2010 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
17package com.android.browser;
18
19import android.app.Fragment;
Ben Murdochaf554522010-09-10 22:09:30 +010020import android.os.Bundle;
Ben Murdoch23da30e2010-10-26 15:18:44 +010021import android.os.Handler;
22import android.os.Message;
Ben Murdochaf554522010-09-10 22:09:30 +010023import android.util.Log;
24import android.view.View;
25import android.view.ViewGroup;
26import android.view.View.OnClickListener;
27import android.view.LayoutInflater;
Ben Murdoch0cb81892010-10-08 12:41:33 +010028import android.webkit.WebSettings.AutoFillProfile;
Ben Murdochaf554522010-09-10 22:09:30 +010029import android.widget.Button;
30import android.widget.EditText;
Ben Murdoch36a23dd2010-10-13 13:20:06 +010031import android.widget.Toast;
Ben Murdochaf554522010-09-10 22:09:30 +010032
33public class AutoFillSettingsFragment extends Fragment {
34
35 private static final String LOGTAG = "AutoFillSettingsFragment";
36
Ben Murdoch36a23dd2010-10-13 13:20:06 +010037 private EditText mFullNameEdit;
38 private EditText mEmailEdit;
39 private EditText mCompanyEdit;
40 private EditText mAddressLine1Edit;
41 private EditText mAddressLine2Edit;
42 private EditText mCityEdit;
43 private EditText mStateEdit;
44 private EditText mZipEdit;
45 private EditText mCountryEdit;
46 private EditText mPhoneEdit;
47
Ben Murdoch23da30e2010-10-26 15:18:44 +010048 // Used to display toast after DB interactions complete.
49 private Handler mHandler;
50
51 private final static int PROFILE_SAVED_MSG = 100;
52 private final static int PROFILE_DELETED_MSG = 101;
53
Ben Murdoch6fa32ba2010-10-20 14:01:25 +010054 // For now we support just one profile so it's safe to hardcode the
55 // id to 1 here. In the future this unique identifier will be set
56 // dynamically.
57 private int mUniqueId = 1;
58
Ben Murdochaf554522010-09-10 22:09:30 +010059 public AutoFillSettingsFragment() {
Ben Murdoch23da30e2010-10-26 15:18:44 +010060 mHandler = new Handler() {
61 @Override
62 public void handleMessage(Message msg) {
63 switch (msg.what) {
64 case PROFILE_SAVED_MSG:
65 Toast.makeText(getActivity(), R.string.autofill_profile_successful_save,
66 Toast.LENGTH_SHORT).show();
67 break;
Ben Murdochaf554522010-09-10 22:09:30 +010068
Ben Murdoch23da30e2010-10-26 15:18:44 +010069 case PROFILE_DELETED_MSG:
70 Toast.makeText(getActivity(), R.string.autofill_profile_successful_delete,
71 Toast.LENGTH_SHORT).show();
72 break;
73 }
74 }
75 };
Ben Murdochaf554522010-09-10 22:09:30 +010076 }
77
78 @Override
79 public void onCreate(Bundle savedState) {
80 super.onCreate(savedState);
81 }
82
83 @Override
84 public View onCreateView(LayoutInflater inflater, ViewGroup container,
Ben Murdoch36a23dd2010-10-13 13:20:06 +010085 Bundle savedInstanceState) {
Ben Murdochaf554522010-09-10 22:09:30 +010086 View v = inflater.inflate(R.layout.autofill_settings_fragment, container, false);
87
Ben Murdoch36a23dd2010-10-13 13:20:06 +010088 mFullNameEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_name_edit);
89 mEmailEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_email_address_edit);
90 mCompanyEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_company_name_edit);
91 mAddressLine1Edit = (EditText)v.findViewById(
92 R.id.autofill_profile_editor_address_line_1_edit);
93 mAddressLine2Edit = (EditText)v.findViewById(
94 R.id.autofill_profile_editor_address_line_2_edit);
95 mCityEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_city_edit);
96 mStateEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_state_edit);
97 mZipEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_zip_code_edit);
98 mCountryEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_country_edit);
99 mPhoneEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_phone_number_edit);
100
Ben Murdochaf554522010-09-10 22:09:30 +0100101 Button saveButton = (Button)v.findViewById(R.id.autofill_profile_editor_save_button);
102 saveButton.setOnClickListener(new OnClickListener() {
103 public void onClick(View button) {
Ben Murdoch23da30e2010-10-26 15:18:44 +0100104 AutoFillProfile newProfile = new AutoFillProfile(
105 mUniqueId,
106 mFullNameEdit.getText().toString(),
107 mEmailEdit.getText().toString(),
108 mCompanyEdit.getText().toString(),
109 mAddressLine1Edit.getText().toString(),
110 mAddressLine2Edit.getText().toString(),
111 mCityEdit.getText().toString(),
112 mStateEdit.getText().toString(),
113 mZipEdit.getText().toString(),
114 mCountryEdit.getText().toString(),
115 mPhoneEdit.getText().toString());
116
117 BrowserSettings.getInstance().setAutoFillProfile(getActivity(), newProfile,
118 mHandler.obtainMessage(PROFILE_SAVED_MSG));
Ben Murdochaf554522010-09-10 22:09:30 +0100119 }
120 });
121
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100122 Button deleteButton = (Button)v.findViewById(R.id.autofill_profile_editor_delete_button);
123 deleteButton.setOnClickListener(new OnClickListener() {
124 public void onClick(View button) {
Ben Murdoch23da30e2010-10-26 15:18:44 +0100125 // Clear the UI.
126 mFullNameEdit.setText("");
127 mEmailEdit.setText("");
128 mCompanyEdit.setText("");
129 mAddressLine1Edit.setText("");
130 mAddressLine2Edit.setText("");
131 mCityEdit.setText("");
132 mStateEdit.setText("");
133 mZipEdit.setText("");
134 mCountryEdit.setText("");
135 mPhoneEdit.setText("");
136
137 // Update browser settings and native with a null profile. This will
138 // trigger the current profile to get deleted from the DB.
139 BrowserSettings.getInstance().setAutoFillProfile(getActivity(), null,
140 mHandler.obtainMessage(PROFILE_DELETED_MSG));
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100141 }
142 });
143
144 Button cancelButton = (Button)v.findViewById(R.id.autofill_profile_editor_cancel_button);
145 cancelButton.setOnClickListener(new OnClickListener() {
146 public void onClick(View button) {
147 getFragmentManager().popBackStack();
148 }
149 });
150
Ben Murdoch0cb81892010-10-08 12:41:33 +0100151 // Populate the text boxes with any pre existing AutoFill data.
Ben Murdoch0cb81892010-10-08 12:41:33 +0100152 AutoFillProfile activeProfile = BrowserSettings.getInstance().getAutoFillProfile();
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100153 if (activeProfile != null) {
154 mFullNameEdit.setText(activeProfile.getFullName());
155 mEmailEdit.setText(activeProfile.getEmailAddress());
156 mCompanyEdit.setText(activeProfile.getCompanyName());
157 mAddressLine1Edit.setText(activeProfile.getAddressLine1());
158 mAddressLine2Edit.setText(activeProfile.getAddressLine2());
159 mCityEdit.setText(activeProfile.getCity());
160 mStateEdit.setText(activeProfile.getState());
161 mZipEdit.setText(activeProfile.getZipCode());
162 mCountryEdit.setText(activeProfile.getCountry());
163 mPhoneEdit.setText(activeProfile.getPhoneNumber());
164 }
Ben Murdochaf554522010-09-10 22:09:30 +0100165
166 return v;
167 }
Ben Murdochaf554522010-09-10 22:09:30 +0100168}