OSDN Git Service

0a0be4fe1d6dda91f531ee0c51be9ce0cc906b40
[android-x86/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.Intent;
27 import android.content.SharedPreferences;
28 import android.net.Uri;
29 import android.os.Bundle;
30 import android.preference.PreferenceManager;
31
32 import com.google.android.gsf.GoogleLoginServiceConstants;
33
34 import java.io.IOException;
35
36
37 public class LaunchActivity extends Activity {
38     static final String KEY_DETAIL_VIEW = "DETAIL_VIEW";
39     static final String GMAIL_AUTH_SERVICE = "mail";
40
41     private Bundle mExtras;
42
43     @Override
44     protected void onCreate(Bundle icicle) {
45         super.onCreate(icicle);
46         mExtras = getIntent().getExtras();
47
48         // Our UI is not something intended for the user to see.  We just
49         // stick around until we can figure out what to do next based on
50         // the current state of the system.
51         // TODO: Removed until framework is fixed in b/2008662
52         // setVisible(false);
53
54         // Only try looking for an account if this is the first launch.
55         if (icicle == null) {
56             // This will request a Gmail account and if none are present, it will
57             // invoke SetupWizard to login or create one. The result is returned
58             // via the Future2Callback.
59             Bundle bundle = new Bundle();
60             bundle.putCharSequence("optional_message", getText(R.string.calendar_plug));
61             AccountManager.get(this).getAuthTokenByFeatures(
62                     GoogleLoginServiceConstants.ACCOUNT_TYPE, GMAIL_AUTH_SERVICE,
63                     new String[]{GoogleLoginServiceConstants.FEATURE_LEGACY_HOSTED_OR_GOOGLE}, this,
64                     bundle, null /* loginOptions */, new AccountManagerCallback<Bundle>() {
65                 public void run(AccountManagerFuture<Bundle> future) {
66                     try {
67                         Bundle result = future.getResult();
68                         onAccountsLoaded(new Account(
69                                 result.getString(GoogleLoginServiceConstants.AUTH_ACCOUNT_KEY),
70                                 result.getString(AccountManager.KEY_ACCOUNT_TYPE)));
71                     } catch (OperationCanceledException e) {
72                         finish();
73                     } catch (IOException e) {
74                         finish();
75                     } catch (AuthenticatorException e) {
76                         finish();
77                     }
78                 }
79             }, null /* handler */);
80         }
81     }
82
83     private void onAccountsLoaded(Account account) {
84         // Get the data for from this intent, if any
85         Intent myIntent = getIntent();
86         Uri myData = myIntent.getData();
87
88         // Set up the intent for the start activity
89         Intent intent = new Intent();
90         if (myData != null) {
91             intent.setData(myData);
92         }
93
94         String defaultViewKey = CalendarPreferenceActivity.KEY_START_VIEW;
95         if (mExtras != null) {
96             intent.putExtras(mExtras);
97             if (mExtras.getBoolean(KEY_DETAIL_VIEW, false)) {
98                 defaultViewKey = CalendarPreferenceActivity.KEY_DETAILED_VIEW;
99             }
100         }
101         intent.putExtras(myIntent);
102
103         SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
104         String startActivity = prefs.getString(defaultViewKey,
105                 CalendarPreferenceActivity.DEFAULT_START_VIEW);
106
107         intent.setClassName(this, startActivity);
108         intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
109         startActivity(intent);
110         finish();
111     }
112 }