+package eu.alefzero.owncloud.ui.fragment;\r
+\r
+import android.accounts.Account;\r
+import android.accounts.AccountManager;\r
+import android.content.Context;\r
+import android.content.Intent;\r
+import android.os.Bundle;\r
+import android.support.v4.app.Fragment;\r
+import android.util.Log;\r
+import android.view.LayoutInflater;\r
+import android.view.View;\r
+import android.view.ViewGroup;\r
+import android.widget.AdapterView;\r
+import android.widget.AdapterView.OnItemClickListener;\r
+import android.widget.BaseAdapter;\r
+import android.widget.GridView;\r
+import android.widget.ImageView;\r
+import android.widget.TextView;\r
+import android.widget.Toast;\r
+import eu.alefzero.owncloud.R;\r
+import eu.alefzero.owncloud.authenticator.AccountAuthenticator;\r
+import eu.alefzero.owncloud.ui.FileDisplayActivity;\r
+import eu.alefzero.owncloud.ui.Preferences;\r
+\r
+public class LandingPageFragment extends Fragment implements OnItemClickListener {\r
+\r
+ @Override\r
+ public View onCreateView(LayoutInflater inflater, ViewGroup container,\r
+ Bundle savedInstanceState) {\r
+ View root = inflater.inflate(R.layout.landing_page_fragment, container);\r
+ return root;\r
+ }\r
+\r
+\r
+ @Override\r
+ public void onActivityCreated(Bundle savedInstanceState) {\r
+ super.onActivityCreated(savedInstanceState);\r
+ GridView grid = (GridView) getView().findViewById(R.id.gridview);\r
+ grid.setAdapter(new LandingScreenAdapter(getActivity()));\r
+ grid.setOnItemClickListener(this);\r
+ }\r
+ \r
+ @Override\r
+ public void onItemClick(AdapterView<?> parent, View view, int position, long id) {\r
+ Intent intent;\r
+ \r
+ /**\r
+ * If the user selects something and acounts are setup,\r
+ * we can use our LandingScreenAdapter to get the matching\r
+ * intent for the selected item.\r
+ * \r
+ * Otherwise, the accounsAreSetuo() method will trigger the \r
+ * creation of one.\r
+ */\r
+ if(accountsAreSetup()){\r
+ intent = (Intent) parent.getAdapter().getItem(position);\r
+ if(intent != null ){\r
+ startActivity(intent);\r
+ } else {\r
+ Toast toast = Toast.makeText(getActivity(), "Not yet implemented!", Toast.LENGTH_SHORT);\r
+ toast.show();\r
+ }\r
+ \r
+ } \r
+ }\r
+\r
+ /**\r
+ * Checks, whether or not there are any ownCloud accounts \r
+ * setup. If there is none, it will create one.\r
+ * \r
+ * If there are more then one, it will trigger a selection\r
+ * unless the selection has not been made yet.\r
+ * \r
+ * @return true, if there is at least one account.\r
+ */\r
+ private boolean accountsAreSetup() {\r
+ AccountManager accMan = AccountManager.get(getActivity());\r
+ Account[] accounts = accMan\r
+ .getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);\r
+\r
+ if (accounts.length == 0) {\r
+ Intent intent = new Intent("android.settings.ADD_ACCOUNT_SETTINGS");\r
+ intent.putExtra("authorities",\r
+ new String[] { AccountAuthenticator.AUTH_TOKEN_TYPE });\r
+ startActivity(intent);\r
+ return false;\r
+ } else if (accounts.length > 1) {\r
+ // TODO: Figure out what to do.\r
+ } \r
+\r
+ return true;\r
+ }\r
+\r
+ /**\r
+ * Used to populate the landing page grid.\r
+ * Defined this one right in here as private class\r
+ * as it is unlikely that this Adapter can be useful\r
+ * anywhere else.\r
+ * \r
+ * @author Lennart Rosam\r
+ *\r
+ */\r
+ private class LandingScreenAdapter extends BaseAdapter {\r
+\r
+ private Context mContext;\r
+\r
+ private final Integer[] mLandingScreenIcons = { R.drawable.home,\r
+ R.drawable.music, R.drawable.contacts,\r
+ android.R.drawable.ic_menu_today,\r
+ android.R.drawable.ic_menu_agenda,\r
+ android.R.drawable.ic_menu_preferences };\r
+\r
+ private final Integer[] mLandingScreenTexts = { R.string.main_files,\r
+ R.string.main_music, R.string.main_contacts,\r
+ R.string.main_calendar, R.string.main_bookmarks,\r
+ R.string.main_settings };\r
+\r
+ public LandingScreenAdapter(Context context) {\r
+ mContext = context;\r
+ }\r
+\r
+ @Override\r
+ public int getCount() {\r
+ return mLandingScreenIcons.length;\r
+ }\r
+\r
+ @Override\r
+ /**\r
+ * Returns the Intent associated with this object\r
+ * or null if the functionality is not yet implemented\r
+ */\r
+ public Object getItem(int position) {\r
+ Intent intent = new Intent();\r
+ switch (position) {\r
+ case 0:\r
+ intent.setClass(mContext, FileDisplayActivity.class);\r
+ break;\r
+ case 5:\r
+ intent.setClass(mContext, Preferences.class);\r
+ break;\r
+ default:\r
+ intent = null;\r
+ }\r
+ return intent;\r
+ }\r
+\r
+ @Override\r
+ public long getItemId(int position) {\r
+ return position;\r
+ }\r
+\r
+ @Override\r
+ public View getView(int position, View convertView, ViewGroup parent) {\r
+ if (convertView == null) {\r
+ LayoutInflater inflator = LayoutInflater.from(mContext);\r
+ convertView = inflator\r
+ .inflate(R.layout.landing_page_item, null);\r
+\r
+ ImageView icon = (ImageView) convertView\r
+ .findViewById(R.id.gridImage);\r
+ TextView iconText = (TextView) convertView\r
+ .findViewById(R.id.gridText);\r
+\r
+ icon.setImageResource(mLandingScreenIcons[position]);\r
+ iconText.setText(mLandingScreenTexts[position]);\r
+ }\r
+ return convertView;\r
+ }\r
+\r
+ }\r
+}\r