Disable change log; to remove in future
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / LandingActivity.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 */
18 package com.owncloud.android.ui.activity;
19
20 import com.actionbarsherlock.app.SherlockFragmentActivity;
21 import com.owncloud.android.authenticator.AccountAuthenticator;
22 import com.owncloud.android.ui.adapter.LandingScreenAdapter;
23
24 import android.accounts.Account;
25 import android.accounts.AccountManager;
26 import android.app.AlertDialog;
27 import android.app.Dialog;
28 import android.content.DialogInterface;
29 import android.content.DialogInterface.OnClickListener;
30 import android.content.Intent;
31 import android.os.Bundle;
32 import android.view.View;
33 import android.widget.AdapterView;
34 import android.widget.AdapterView.OnItemClickListener;
35 import android.widget.GridView;
36 import android.widget.Toast;
37 import com.owncloud.android.R;
38
39 /**
40 * This activity is used as a landing page when the user first opens this app.
41 *
42 * @author Lennart Rosam
43 *
44 */
45 public class LandingActivity extends SherlockFragmentActivity implements
46 OnClickListener, OnItemClickListener {
47
48 public static final int DIALOG_SETUP_ACCOUNT = 1;
49
50 @Override
51 protected void onCreate(Bundle savedInstanceState) {
52 super.onCreate(savedInstanceState);
53 setContentView(R.layout.main);
54
55 // Fill the grid view of the landing screen with icons
56 GridView landingScreenItems = (GridView) findViewById(R.id.homeScreenGrid);
57 landingScreenItems.setAdapter(new LandingScreenAdapter(this));
58 landingScreenItems.setOnItemClickListener(this);
59
60 // Check, if there are ownCloud accounts
61 if (!accountsAreSetup()) {
62 showDialog(DIALOG_SETUP_ACCOUNT);
63 } else {
64 // Start device tracking service
65 Intent locationServiceIntent = new Intent();
66 locationServiceIntent
67 .setAction("com.owncloud.android.location.LocationLauncher");
68 sendBroadcast(locationServiceIntent);
69 }
70
71 }
72
73 @Override
74 protected void onRestart() {
75 super.onRestart();
76 // Check, if there are ownCloud accounts
77 if (!accountsAreSetup()) {
78 showDialog(DIALOG_SETUP_ACCOUNT);
79 }
80 }
81
82 @Override
83 protected void onRestoreInstanceState(Bundle savedInstanceState) {
84 super.onRestoreInstanceState(savedInstanceState);
85 // Check, if there are ownCloud accounts
86 if (!accountsAreSetup()) {
87 showDialog(DIALOG_SETUP_ACCOUNT);
88 }
89 }
90
91 @Override
92 protected Dialog onCreateDialog(int id) {
93 Dialog dialog;
94 switch (id) {
95 case DIALOG_SETUP_ACCOUNT:
96 AlertDialog.Builder builder = new AlertDialog.Builder(this);
97 builder.setTitle(R.string.main_tit_accsetup);
98 builder.setMessage(R.string.main_wrn_accsetup);
99 builder.setCancelable(false);
100 builder.setPositiveButton(R.string.common_ok, this);
101 builder.setNegativeButton(R.string.common_cancel, this);
102 dialog = builder.create();
103 break;
104 default:
105 dialog = null;
106 }
107
108 return dialog;
109 }
110
111 public void onClick(DialogInterface dialog, int which) {
112 // In any case - we won't need it anymore
113 dialog.dismiss();
114 switch (which) {
115 case DialogInterface.BUTTON_POSITIVE:
116 Intent intent = new Intent("android.settings.ADD_ACCOUNT_SETTINGS");
117 intent.putExtra("authorities",
118 new String[] { AccountAuthenticator.AUTH_TOKEN_TYPE });
119 startActivity(intent);
120 break;
121 case DialogInterface.BUTTON_NEGATIVE:
122 finish();
123 }
124
125 }
126
127 @Override
128 /**
129 * Start an activity based on the selection
130 * the user made
131 */
132 public void onItemClick(AdapterView<?> parent, View view, int position,
133 long id) {
134 Intent intent;
135 intent = (Intent) parent.getAdapter().getItem(position);
136 if (intent != null) {
137 startActivity(intent);
138 } else {
139 // TODO: Implement all of this and make this text go away ;-)
140 Toast toast = Toast.makeText(this, "Not yet implemented!",
141 Toast.LENGTH_SHORT);
142 toast.show();
143 }
144 }
145
146 /**
147 * Checks, whether or not there are any ownCloud accounts setup.
148 *
149 * @return true, if there is at least one account.
150 */
151 private boolean accountsAreSetup() {
152 AccountManager accMan = AccountManager.get(this);
153 Account[] accounts = accMan
154 .getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
155 return accounts.length > 0;
156 }
157
158 }