blob: b650edeac48431a3a781644e8ba377c4a797d233 [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;
20import android.database.Cursor;
21import android.database.sqlite.SQLiteDatabase;
22import android.os.AsyncTask;
23import android.os.Bundle;
24import android.util.Log;
25import android.view.View;
26import android.view.ViewGroup;
27import android.view.View.OnClickListener;
28import android.view.LayoutInflater;
29import android.widget.Button;
30import android.widget.EditText;
31import android.widget.Toast;
32
33public class AutoFillSettingsFragment extends Fragment {
34
35 private static final String LOGTAG = "AutoFillSettingsFragment";
36
37 // TODO: This will become dynamic once we support more than one profile.
38 private int mProfileId = 1;
39
40 public AutoFillSettingsFragment() {
41
42 }
43
44 @Override
45 public void onCreate(Bundle savedState) {
46 super.onCreate(savedState);
47 }
48
49 @Override
50 public View onCreateView(LayoutInflater inflater, ViewGroup container,
51 Bundle savedInstanceState) {
52 View v = inflater.inflate(R.layout.autofill_settings_fragment, container, false);
53
54 Button saveButton = (Button)v.findViewById(R.id.autofill_profile_editor_save_button);
55 saveButton.setOnClickListener(new OnClickListener() {
56 public void onClick(View button) {
57 View v = getView();
58 EditText fullName = (EditText)v.findViewById(
59 R.id.autofill_profile_editor_name_edit);
60 EditText email = (EditText)v.findViewById(
61 R.id.autofill_profile_editor_email_address_edit);
62 new SaveProfileToDbTask().execute(fullName.getText().toString(),
63 email.getText().toString());
64 }
65 });
66
67 // Load the profile and populate the text views in the background
68 new LoadProfileFromDbTask().execute(mProfileId);
69
70 return v;
71 }
72
73 @Override
74 public void onPause() {
75 AutoFillProfileDatabase db =
76 AutoFillProfileDatabase.getInstance(getActivity());
77 db.close();
78 super.onPause();
79 }
80
81 private class SaveProfileToDbTask extends AsyncTask<String, Void, Void> {
82 protected Void doInBackground(String... values) {
83 AutoFillProfileDatabase db =
84 AutoFillProfileDatabase.getInstance(getActivity());
85 db.addOrUpdateProfile(mProfileId, values[0], values[1]);
86 return null;
87 }
88
89 protected void onPostExecute(Void result) {
90 Toast.makeText(getActivity(), "Saved profile", Toast.LENGTH_SHORT).show();
91 }
92 }
93
94 private static class LoadedProfileData {
95 private String mFullName;
96 private String mEmailAddress;
97
98 public LoadedProfileData(String fullName, String emailAddress) {
99 mFullName = fullName;
100 mEmailAddress = emailAddress;
101 }
102
103 public String getFullName() { return mFullName; }
104 public String getEmailAddress() { return mEmailAddress; }
105 }
106
107 private class LoadProfileFromDbTask extends AsyncTask<Integer, Void, LoadedProfileData> {
108 protected LoadedProfileData doInBackground(Integer... id) {
109 AutoFillProfileDatabase db = AutoFillProfileDatabase.getInstance(getActivity());
110 Cursor c = db.getProfile(id[0]);
111 c.moveToFirst();
112
113 LoadedProfileData profileData = null;
114
115 if (c.getCount() > 0) {
116 String fullName = c.getString(c.getColumnIndex(
117 AutoFillProfileDatabase.Profiles.FULL_NAME));
118 String email = c.getString(c.getColumnIndex(
119 AutoFillProfileDatabase.Profiles.EMAIL_ADDRESS));
120 profileData = new LoadedProfileData(fullName, email);
121 }
122 c.close();
123 return profileData;
124 }
125
126 protected void onPostExecute(LoadedProfileData data) {
127 if (data == null) {
128 return;
129 }
130
131 View v = getView();
132 if (v != null) {
133 EditText fullName = (EditText)v.findViewById(
134 R.id.autofill_profile_editor_name_edit);
135 EditText email = (EditText)v.findViewById(
136 R.id.autofill_profile_editor_email_address_edit);
137 fullName.setText(data.getFullName());
138 email.setText(data.getEmailAddress());
139 }
140 }
141 }
142}