7e2a8c034591318a4f56a78bf1cd871571c6117c
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / 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 3 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 eu.alefzero.owncloud.ui.activity;
19
20 import com.actionbarsherlock.app.SherlockFragmentActivity;
21
22 import android.accounts.Account;
23 import android.accounts.AccountManager;
24 import android.app.AlertDialog;
25 import android.app.Dialog;
26 import android.content.DialogInterface;
27 import android.content.DialogInterface.OnClickListener;
28 import android.content.Intent;
29 import android.os.Bundle;
30 import android.view.View;
31 import android.widget.AdapterView;
32 import android.widget.AdapterView.OnItemClickListener;
33 import android.widget.GridView;
34 import android.widget.Toast;
35 import eu.alefzero.owncloud.R;
36 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
37 import eu.alefzero.owncloud.ui.adapter.LandingScreenAdapter;
38
39 /**
40 * This activity is used as a landing page when the user first opens this app.
41 * @author Lennart Rosam
42 *
43 */
44 public class LandingActivity extends SherlockFragmentActivity implements OnClickListener, OnItemClickListener {
45
46 public static final int DIALOG_SETUP_ACCOUNT = 1;
47
48 @Override
49 protected void onCreate(Bundle savedInstanceState) {
50 super.onCreate(savedInstanceState);
51 setContentView(R.layout.main);
52
53 // Fill the grid view of the landing screen with icons
54 GridView landingScreenItems = (GridView) findViewById(R.id.homeScreenGrid);
55 landingScreenItems.setAdapter(new LandingScreenAdapter(this));
56 landingScreenItems.setOnItemClickListener(this);
57
58 // Check, if there are ownCloud accounts
59 if(!accountsAreSetup()){
60 showDialog(DIALOG_SETUP_ACCOUNT);
61 }
62
63 }
64
65 @Override
66 protected void onRestart() {
67 super.onRestart();
68 // Check, if there are ownCloud accounts
69 if(!accountsAreSetup()){
70 showDialog(DIALOG_SETUP_ACCOUNT);
71 }
72 }
73
74 @Override
75 protected void onRestoreInstanceState(Bundle savedInstanceState) {
76 super.onRestoreInstanceState(savedInstanceState);
77 // Check, if there are ownCloud accounts
78 if(!accountsAreSetup()){
79 showDialog(DIALOG_SETUP_ACCOUNT);
80 }
81 }
82
83 @Override
84 protected Dialog onCreateDialog(int id) {
85 Dialog dialog;
86 switch(id){
87 case DIALOG_SETUP_ACCOUNT:
88 AlertDialog.Builder builder = new AlertDialog.Builder(this);
89 builder.setTitle(R.string.main_tit_accsetup);
90 builder.setMessage(R.string.main_wrn_accsetup);
91 builder.setCancelable(false);
92 builder.setPositiveButton(R.string.common_ok, this);
93 builder.setNegativeButton(R.string.common_cancel, this);
94 dialog = builder.create();
95 break;
96 default:
97 dialog = null;
98 }
99
100 return dialog;
101 }
102
103 public void onClick(DialogInterface dialog, int which) {
104 // In any case - we won't need it anymore
105 dialog.dismiss();
106 switch(which){
107 case DialogInterface.BUTTON_POSITIVE:
108 Intent intent = new Intent("android.settings.ADD_ACCOUNT_SETTINGS");
109 intent.putExtra("authorities",
110 new String[] { AccountAuthenticator.AUTH_TOKEN_TYPE });
111 startActivity(intent);
112 break;
113 case DialogInterface.BUTTON_NEGATIVE:
114 finish();
115 }
116
117 }
118
119 @Override
120 /**
121 * Start an activity based on the selection
122 * the user made
123 */
124 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
125 Intent intent;
126 intent = (Intent) parent.getAdapter().getItem(position);
127 if(intent != null ){
128 startActivity(intent);
129 } else {
130 // TODO: Implement all of this and make this text go away ;-)
131 Toast toast = Toast.makeText(this, "Not yet implemented!", Toast.LENGTH_SHORT);
132 toast.show();
133 }
134 }
135
136 /**
137 * Checks, whether or not there are any ownCloud accounts
138 * setup.
139 *
140 * @return true, if there is at least one account.
141 */
142 private boolean accountsAreSetup() {
143 AccountManager accMan = AccountManager.get(this);
144 Account[] accounts = accMan
145 .getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
146 return accounts.length > 0;
147 }
148
149
150 }