Ben Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.browser; |
| 18 | |
| 19 | import android.app.Fragment; |
Ben Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 20 | import android.os.Bundle; |
Ben Murdoch | 23da30e | 2010-10-26 15:18:44 +0100 | [diff] [blame] | 21 | import android.os.Handler; |
| 22 | import android.os.Message; |
Ben Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 23 | import android.util.Log; |
| 24 | import android.view.View; |
| 25 | import android.view.ViewGroup; |
| 26 | import android.view.View.OnClickListener; |
| 27 | import android.view.LayoutInflater; |
Ben Murdoch | 0cb8189 | 2010-10-08 12:41:33 +0100 | [diff] [blame] | 28 | import android.webkit.WebSettings.AutoFillProfile; |
Ben Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 29 | import android.widget.Button; |
| 30 | import android.widget.EditText; |
Ben Murdoch | 36a23dd | 2010-10-13 13:20:06 +0100 | [diff] [blame] | 31 | import android.widget.Toast; |
Ben Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 32 | |
| 33 | public class AutoFillSettingsFragment extends Fragment { |
| 34 | |
| 35 | private static final String LOGTAG = "AutoFillSettingsFragment"; |
| 36 | |
Ben Murdoch | 36a23dd | 2010-10-13 13:20:06 +0100 | [diff] [blame] | 37 | 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 Murdoch | 23da30e | 2010-10-26 15:18:44 +0100 | [diff] [blame] | 48 | // 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 Murdoch | 6fa32ba | 2010-10-20 14:01:25 +0100 | [diff] [blame] | 54 | // 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 Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 59 | public AutoFillSettingsFragment() { |
Ben Murdoch | 23da30e | 2010-10-26 15:18:44 +0100 | [diff] [blame] | 60 | 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 Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 68 | |
Ben Murdoch | 23da30e | 2010-10-26 15:18:44 +0100 | [diff] [blame] | 69 | 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 Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 76 | } |
| 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 Murdoch | 36a23dd | 2010-10-13 13:20:06 +0100 | [diff] [blame] | 85 | Bundle savedInstanceState) { |
Ben Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 86 | View v = inflater.inflate(R.layout.autofill_settings_fragment, container, false); |
| 87 | |
Ben Murdoch | 36a23dd | 2010-10-13 13:20:06 +0100 | [diff] [blame] | 88 | 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 Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 101 | Button saveButton = (Button)v.findViewById(R.id.autofill_profile_editor_save_button); |
| 102 | saveButton.setOnClickListener(new OnClickListener() { |
| 103 | public void onClick(View button) { |
Ben Murdoch | 23da30e | 2010-10-26 15:18:44 +0100 | [diff] [blame] | 104 | 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 Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 119 | } |
| 120 | }); |
| 121 | |
Ben Murdoch | 36a23dd | 2010-10-13 13:20:06 +0100 | [diff] [blame] | 122 | Button deleteButton = (Button)v.findViewById(R.id.autofill_profile_editor_delete_button); |
| 123 | deleteButton.setOnClickListener(new OnClickListener() { |
| 124 | public void onClick(View button) { |
Ben Murdoch | 23da30e | 2010-10-26 15:18:44 +0100 | [diff] [blame] | 125 | // 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 Murdoch | 36a23dd | 2010-10-13 13:20:06 +0100 | [diff] [blame] | 141 | } |
| 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 Murdoch | 0cb8189 | 2010-10-08 12:41:33 +0100 | [diff] [blame] | 151 | // Populate the text boxes with any pre existing AutoFill data. |
Ben Murdoch | 0cb8189 | 2010-10-08 12:41:33 +0100 | [diff] [blame] | 152 | AutoFillProfile activeProfile = BrowserSettings.getInstance().getAutoFillProfile(); |
Ben Murdoch | 36a23dd | 2010-10-13 13:20:06 +0100 | [diff] [blame] | 153 | 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 Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 165 | |
| 166 | return v; |
| 167 | } |
Ben Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 168 | } |