summaryrefslogtreecommitdiffstats
path: root/src/com/android/calendar/LaunchActivity.java
blob: d68805974f15d348856466494ee537b01c16e3b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnClickListener;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.Calendar.Calendars;

public class LaunchActivity extends Activity implements OnCancelListener,
        OnClickListener, Runnable {

    private static final String[] PROJECTION = new String[] {
        Calendars._ID,
    };
    
    public void run() {
        /* Start a query to refresh the list of calendars if for some reason
         * the list was not fetched from the server.  We don't care about
         * the contents of the returned cursor; we do the query strictly for
         * the side-effect of refreshing the list of calendars from the server.
         */
        final ContentResolver cr = getContentResolver();
        Cursor cursor = cr.query(Calendars.LIVE_CONTENT_URI, PROJECTION,
            null, null, null);
        
        if (cursor != null) {
            cursor.close();
        }
    }
    
    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        
        // Check to see if there are no calendars
        final ContentResolver cr = getContentResolver();
        Cursor cursor = cr.query(Calendars.CONTENT_URI, PROJECTION,
                null /* selection */,
                null /* selectionArgs */,
                Calendars.DEFAULT_SORT_ORDER);
        
        boolean missingCalendars = false;
        if ((cursor == null) || (cursor.getCount() == 0)) {
            missingCalendars = true;
        }
        
        if (cursor != null) {
            cursor.close();
        }
        
        if (missingCalendars) {
            new AlertDialog.Builder(this)
                    .setTitle(R.string.no_calendars)
                    .setMessage(R.string.no_calendars_msg)
                    .setCancelable(true)
                    .setOnCancelListener(this)
                    .setPositiveButton(R.string.ok_label, this)
                    .show();
            new Thread(this).start();
            return;
        }
            
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        String startActivity = prefs.getString(CalendarPreferenceActivity.KEY_START_VIEW,
                CalendarPreferenceActivity.DEFAULT_START_VIEW);
            
        // Get the data for from this intent, if any
        Intent myIntent = getIntent();
        Uri myData = myIntent.getData();
            
        // Set up the intent for the start activity
        Intent intent = new Intent();
        if (myData != null) {
            intent.setData(myData);
        }
        intent.setClassName(this, startActivity);
        startActivity(intent);
        finish();
    }

    public void onCancel(DialogInterface dialog) {
        finish();
    }

    public void onClick(DialogInterface dialog, int which) {
        finish();
    }
}