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