revive a hack that creates a local calendar if the google account authenticator is...
[android/platform/packages/apps/Calendar.git] / src / com / android / calendar / LaunchActivity.java
1 /*
2  * Copyright (C) 2007 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.calendar;
18
19 import android.accounts.Account;
20 import android.accounts.AccountManager;
21 import android.accounts.AccountManagerCallback;
22 import android.accounts.AccountManagerFuture;
23 import android.accounts.AuthenticatorException;
24 import android.accounts.OperationCanceledException;
25 import android.app.Activity;
26 import android.content.ContentValues;
27 import android.content.Intent;
28 import android.content.SharedPreferences;
29 import android.database.Cursor;
30 import android.net.Uri;
31 import android.os.Bundle;
32 import android.preference.PreferenceManager;
33 import android.provider.Calendar.Calendars;
34 import android.provider.Gmail;
35 import android.util.Log;
36
37 import com.google.android.googlelogin.GoogleLoginServiceConstants;
38
39 import java.io.IOException;
40
41 import java.util.TimeZone;
42
43 public class LaunchActivity extends Activity {
44     static final String KEY_DETAIL_VIEW = "DETAIL_VIEW";
45     private Bundle mExtras;
46
47     @Override
48     protected void onCreate(Bundle icicle) {
49         super.onCreate(icicle);
50         mExtras = getIntent().getExtras();
51
52         // Our UI is not something intended for the user to see.  We just
53         // stick around until we can figure out what to do next based on
54         // the current state of the system.
55         // TODO: Removed until framework is fixed in b/2008662
56         // setVisible(false);
57
58         // Only try looking for an account if this is the first launch.
59         if (icicle == null) {
60             // This will request a Gmail account and if none are present, it will
61             // invoke SetupWizard to login or create one. The result is returned
62             // via the Future2Callback.
63             Bundle bundle = new Bundle();
64             bundle.putCharSequence("optional_message", getText(R.string.calendar_plug));
65             AccountManager.get(this).getAuthTokenByFeatures(
66                     GoogleLoginServiceConstants.ACCOUNT_TYPE, Gmail.GMAIL_AUTH_SERVICE,
67                     new String[]{GoogleLoginServiceConstants.FEATURE_LEGACY_HOSTED_OR_GOOGLE}, this,
68                     bundle, null /* loginOptions */, new AccountManagerCallback<Bundle>() {
69                 public void run(AccountManagerFuture<Bundle> future) {
70                     try {
71                         Bundle result = future.getResult();
72                         onAccountsLoaded(new Account(
73                                 result.getString(GoogleLoginServiceConstants.AUTH_ACCOUNT_KEY),
74                                 result.getString(AccountManager.KEY_ACCOUNT_TYPE)));
75                     } catch (OperationCanceledException e) {
76                         finish();
77                     } catch (IOException e) {
78                         finish();
79                     } catch (AuthenticatorException e) {
80                         Account account = maybeCreateLocalCalendar();
81                         onAccountsLoaded(account);
82                     }
83                 }
84             }, null /* handler */);
85         }
86     }
87
88     private Account maybeCreateLocalCalendar() {
89         Account localAccount = new Account("nobody@localhost", "localhost");
90         // create a local calendar if there isn't one already
91         Cursor cur = getContentResolver().query(Calendars.CONTENT_URI,
92                 null, null, null, null);
93         try {
94             if (cur.getCount() != 0) {
95                 cur.moveToFirst();
96                 try {
97                     String accountName =
98                         cur.getString(cur.getColumnIndexOrThrow(Calendars._SYNC_ACCOUNT));
99                     String accountType =
100                         cur.getString(cur.getColumnIndexOrThrow(Calendars._SYNC_ACCOUNT_TYPE));
101                     if (accountName == null) {
102                         return localAccount;
103                     } else {
104                         return new Account(accountName, accountType);
105                     }
106                 } catch(RuntimeException e) {
107                     return null;
108                 }
109             } else {
110                 // inspired from CalendarProvider.onAccountsChanged
111                 ContentValues vals = new ContentValues();
112                 vals.put(Calendars.ACCESS_LEVEL, Integer.toString(Calendars.OWNER_ACCESS));
113                 vals.put(Calendars.COLOR, -14069085);
114                 vals.put(Calendars.DISPLAY_NAME, "Default");
115                 vals.put(Calendars.HIDDEN, 0);
116                 vals.put(Calendars.NAME, "Local");
117                 vals.put(Calendars.SELECTED, 1);
118                 vals.put(Calendars.SYNC_EVENTS, 1);
119                 vals.put(Calendars._SYNC_ACCOUNT, localAccount.name);
120                 vals.put(Calendars._SYNC_ACCOUNT_TYPE, localAccount.type);
121                 vals.put(Calendars.TIMEZONE, TimeZone.getDefault().getID());
122                 getContentResolver().insert(Calendars.CONTENT_URI, vals);
123                 return localAccount;
124             }
125         } finally {
126             cur.close();
127         }
128     }
129
130     private void onAccountsLoaded(Account account) {
131         // Get the data for from this intent, if any
132         Intent myIntent = getIntent();
133         Uri myData = myIntent.getData();
134
135         // Set up the intent for the start activity
136         Intent intent = new Intent();
137         if (myData != null) {
138             intent.setData(myData);
139         }
140
141         String defaultViewKey = CalendarPreferenceActivity.KEY_START_VIEW;
142         if (mExtras != null) {
143             intent.putExtras(mExtras);
144             if (mExtras.getBoolean(KEY_DETAIL_VIEW, false)) {
145                 defaultViewKey = CalendarPreferenceActivity.KEY_DETAILED_VIEW;
146             }
147         }
148         intent.putExtras(myIntent);
149
150         SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
151         String startActivity = prefs.getString(defaultViewKey,
152                 CalendarPreferenceActivity.DEFAULT_START_VIEW);
153
154         intent.setClassName(this, startActivity);
155         intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
156         startActivity(intent);
157         finish();
158     }
159 }