Added a landing page for the app.
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / ui / fragment / LandingPageFragment.java
1 package eu.alefzero.owncloud.ui.fragment;
2
3 import android.accounts.Account;
4 import android.accounts.AccountManager;
5 import android.content.Context;
6 import android.content.Intent;
7 import android.os.Bundle;
8 import android.support.v4.app.Fragment;
9 import android.util.Log;
10 import android.view.LayoutInflater;
11 import android.view.View;
12 import android.view.ViewGroup;
13 import android.widget.AdapterView;
14 import android.widget.AdapterView.OnItemClickListener;
15 import android.widget.BaseAdapter;
16 import android.widget.GridView;
17 import android.widget.ImageView;
18 import android.widget.TextView;
19 import android.widget.Toast;
20 import eu.alefzero.owncloud.R;
21 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
22 import eu.alefzero.owncloud.ui.FileDisplayActivity;
23 import eu.alefzero.owncloud.ui.Preferences;
24
25 public class LandingPageFragment extends Fragment implements OnItemClickListener {
26
27 @Override
28 public View onCreateView(LayoutInflater inflater, ViewGroup container,
29 Bundle savedInstanceState) {
30 View root = inflater.inflate(R.layout.landing_page_fragment, container);
31 return root;
32 }
33
34
35 @Override
36 public void onActivityCreated(Bundle savedInstanceState) {
37 super.onActivityCreated(savedInstanceState);
38 GridView grid = (GridView) getView().findViewById(R.id.gridview);
39 grid.setAdapter(new LandingScreenAdapter(getActivity()));
40 grid.setOnItemClickListener(this);
41 }
42
43 @Override
44 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
45 Intent intent;
46
47 /**
48 * If the user selects something and acounts are setup,
49 * we can use our LandingScreenAdapter to get the matching
50 * intent for the selected item.
51 *
52 * Otherwise, the accounsAreSetuo() method will trigger the
53 * creation of one.
54 */
55 if(accountsAreSetup()){
56 intent = (Intent) parent.getAdapter().getItem(position);
57 if(intent != null ){
58 startActivity(intent);
59 } else {
60 Toast toast = Toast.makeText(getActivity(), "Not yet implemented!", Toast.LENGTH_SHORT);
61 toast.show();
62 }
63
64 }
65 }
66
67 /**
68 * Checks, whether or not there are any ownCloud accounts
69 * setup. If there is none, it will create one.
70 *
71 * If there are more then one, it will trigger a selection
72 * unless the selection has not been made yet.
73 *
74 * @return true, if there is at least one account.
75 */
76 private boolean accountsAreSetup() {
77 AccountManager accMan = AccountManager.get(getActivity());
78 Account[] accounts = accMan
79 .getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
80
81 if (accounts.length == 0) {
82 Intent intent = new Intent("android.settings.ADD_ACCOUNT_SETTINGS");
83 intent.putExtra("authorities",
84 new String[] { AccountAuthenticator.AUTH_TOKEN_TYPE });
85 startActivity(intent);
86 return false;
87 } else if (accounts.length > 1) {
88 // TODO: Figure out what to do.
89 }
90
91 return true;
92 }
93
94 /**
95 * Used to populate the landing page grid.
96 * Defined this one right in here as private class
97 * as it is unlikely that this Adapter can be useful
98 * anywhere else.
99 *
100 * @author Lennart Rosam
101 *
102 */
103 private class LandingScreenAdapter extends BaseAdapter {
104
105 private Context mContext;
106
107 private final Integer[] mLandingScreenIcons = { R.drawable.home,
108 R.drawable.music, R.drawable.contacts,
109 android.R.drawable.ic_menu_today,
110 android.R.drawable.ic_menu_agenda,
111 android.R.drawable.ic_menu_preferences };
112
113 private final Integer[] mLandingScreenTexts = { R.string.main_files,
114 R.string.main_music, R.string.main_contacts,
115 R.string.main_calendar, R.string.main_bookmarks,
116 R.string.main_settings };
117
118 public LandingScreenAdapter(Context context) {
119 mContext = context;
120 }
121
122 @Override
123 public int getCount() {
124 return mLandingScreenIcons.length;
125 }
126
127 @Override
128 /**
129 * Returns the Intent associated with this object
130 * or null if the functionality is not yet implemented
131 */
132 public Object getItem(int position) {
133 Intent intent = new Intent();
134 switch (position) {
135 case 0:
136 intent.setClass(mContext, FileDisplayActivity.class);
137 break;
138 case 5:
139 intent.setClass(mContext, Preferences.class);
140 break;
141 default:
142 intent = null;
143 }
144 return intent;
145 }
146
147 @Override
148 public long getItemId(int position) {
149 return position;
150 }
151
152 @Override
153 public View getView(int position, View convertView, ViewGroup parent) {
154 if (convertView == null) {
155 LayoutInflater inflator = LayoutInflater.from(mContext);
156 convertView = inflator
157 .inflate(R.layout.landing_page_item, null);
158
159 ImageView icon = (ImageView) convertView
160 .findViewById(R.id.gridImage);
161 TextView iconText = (TextView) convertView
162 .findViewById(R.id.gridText);
163
164 icon.setImageResource(mLandingScreenIcons[position]);
165 iconText.setText(mLandingScreenTexts[position]);
166 }
167 return convertView;
168 }
169
170 }
171 }