a12a31c3dd0f268bc54b58e945de6ccba086de1e
[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 that is only available in portrait mode
54 GridView landingScreenItems = (GridView) findViewById(R.id.homeScreenGrid);
55 if(landingScreenItems != null){
56 landingScreenItems.setAdapter(new LandingScreenAdapter(this));
57 landingScreenItems.setOnItemClickListener(this);
58 }
59
60 // Check, if there are ownCloud accounts
61 if(!accountsAreSetup()){
62 showDialog(DIALOG_SETUP_ACCOUNT);
63 }
64
65 }
66
67 @Override
68 protected void onRestart() {
69 super.onRestart();
70 // Check, if there are ownCloud accounts
71 if(!accountsAreSetup()){
72 showDialog(DIALOG_SETUP_ACCOUNT);
73 }
74 }
75
76 @Override
77 protected void onRestoreInstanceState(Bundle savedInstanceState) {
78 super.onRestoreInstanceState(savedInstanceState);
79 // Check, if there are ownCloud accounts
80 if(!accountsAreSetup()){
81 showDialog(DIALOG_SETUP_ACCOUNT);
82 }
83 }
84
85 @Override
86 protected Dialog onCreateDialog(int id) {
87 Dialog dialog;
88 switch(id){
89 case DIALOG_SETUP_ACCOUNT:
90 AlertDialog.Builder builder = new AlertDialog.Builder(this);
91 builder.setTitle(R.string.main_tit_accsetup);
92 builder.setMessage(R.string.main_wrn_accsetup);
93 builder.setCancelable(false);
94 builder.setPositiveButton(R.string.common_ok, this);
95 builder.setNegativeButton(R.string.common_cancel, this);
96 dialog = builder.create();
97 break;
98 default:
99 dialog = null;
100 }
101
102 return dialog;
103 }
104
105 public void onClick(DialogInterface dialog, int which) {
106 // In any case - we won't need it anymore
107 dialog.dismiss();
108 switch(which){
109 case DialogInterface.BUTTON_POSITIVE:
110 Intent intent = new Intent("android.settings.ADD_ACCOUNT_SETTINGS");
111 intent.putExtra("authorities",
112 new String[] { AccountAuthenticator.AUTH_TOKEN_TYPE });
113 startActivity(intent);
114 break;
115 case DialogInterface.BUTTON_NEGATIVE:
116 finish();
117 }
118
119 }
120
121 @Override
122 /**
123 * Start an activity based on the selection
124 * the user made
125 */
126 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
127 Intent intent;
128 intent = (Intent) parent.getAdapter().getItem(position);
129 if(intent != null ){
130 startActivity(intent);
131 } else {
132 Toast toast = Toast.makeText(this, "Not yet implemented!", Toast.LENGTH_SHORT);
133 toast.show();
134 }
135 }
136
137 /**
138 * Checks, whether or not there are any ownCloud accounts
139 * setup.
140 *
141 * @return true, if there is at least one account.
142 */
143 private boolean accountsAreSetup() {
144 AccountManager accMan = AccountManager.get(this);
145 Account[] accounts = accMan
146 .getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
147 return accounts.length > 0;
148 }
149
150
151 }