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