Java tutorial
/* * The MIT License (MIT) * * Copyright 2013 Clover Studio Ltd. All rights reserved. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ package info.hl.mediam; import info.hl.mediam.R; import info.hl.mediam.adapters.GroupCategoriesAdapter; import info.hl.mediam.adapters.GroupsAdapter; import info.hl.mediam.couchdb.CouchDB; import info.hl.mediam.couchdb.mediamException; import info.hl.mediam.couchdb.hlmediaForbiddenException; import info.hl.mediam.couchdb.model.Center; import info.hl.mediam.couchdb.model.CenterCategory; import info.hl.mediam.couchdb.model.CenterSearch; import info.hl.mediam.couchdb.model.Notification; import info.hl.mediam.couchdb.model.RecentActivity; import info.hl.mediam.dialog.HookUpProgressDialog; import info.hl.mediam.extendables.mediamAsync; import info.hl.mediam.extendables.SubMenuActivity; import info.hl.mediam.management.UsersManagement; import info.hl.mediam.utils.Const; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import org.apache.http.client.ClientProtocolException; import org.json.JSONException; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.WindowManager; import android.view.inputmethod.EditorInfo; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.TextView.OnEditorActionListener; /** * GroupsActivity * * Shows a list of groups that user is subscribed to by default; also contains a submenu with * options for searching groups, viewing groups by categories and creating a new * group. */ public class GroupsActivity extends SubMenuActivity { private List<Center> mGroups; private List<CenterCategory> mGroupCategories; private ListView mLvGroups; private ListView mLvGroupCategories; // private Button mBtnCreateGroup; private Button mBtnSearchGroups; private EditText mEtSearchGroups; private GroupsAdapter mGroupListAdapter; private GroupCategoriesAdapter mGroupCategoriesAdapter; private List<Notification> mGroupNotifications; private RelativeLayout mRlSearchGroups; private boolean flagToCreateGroup = false; private RelativeLayout mRlFavoriteGroups; private RelativeLayout mRlSearch; private RelativeLayout mRlCreateGroup; private RelativeLayout mRlCategories; private TextView mTvNoGroups; public static final int REQUEST_UPDATE_GROUPS = 4; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_groups); setSideBar(this.getString(R.string.GROUPS)); Initialization(); OnClickListeners(); showTutorial(getString(R.string.tutorial_groups)); } @Override protected void onResume() { if (flagToCreateGroup) { // if you are comming back from create group then refresh groups // list mTvTitle.setText(getString(R.string.FAVORITES)); new GetGroupsAsync(GroupsActivity.this).execute(FAVORITES); mRlSearchGroups.setVisibility(View.GONE); flagToCreateGroup = false; } super.onResume(); } @Override protected void enableViews() { super.enableViews(); mBtnSearchGroups.setEnabled(true); mEtSearchGroups.setEnabled(true); mLvGroups.setEnabled(true); mLvGroupCategories.setEnabled(true); } @Override protected void disableViews() { super.disableViews(); mBtnSearchGroups.setEnabled(false); mEtSearchGroups.setEnabled(false); mLvGroups.setEnabled(false); mLvGroupCategories.setEnabled(false); } @Override protected void setObjectsNull() { mGroupListAdapter = null; mGroupCategoriesAdapter = null; if (mGroupNotifications != null) { mGroupNotifications.clear(); mGroupNotifications = null; } mLvGroups.setAdapter(null); mLvGroups = null; mLvGroupCategories.setAdapter(null); mLvGroupCategories = null; if (mGroups != null) { mGroups.clear(); mGroups = null; } if (mGroupCategories != null) { mGroupCategories.clear(); mGroupCategories = null; } super.setObjectsNull(); } private void Initialization() { super.setSubMenu(); mLvGroups = (ListView) findViewById(R.id.lvGroups); mLvGroupCategories = (ListView) findViewById(R.id.lvGroupCategories); mLvGroupCategories.setVisibility(View.GONE); mBtnSearchGroups = (Button) findViewById(R.id.btnSearch); mEtSearchGroups = (EditText) findViewById(R.id.etSearchName); mEtSearchGroups.setTypeface(MediamApp.getTfMyriadPro()); mRlSearchGroups = (RelativeLayout) findViewById(R.id.rlSearchGroups); mRlSearchGroups.setVisibility(View.GONE); getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); mRlFavoriteGroups = (RelativeLayout) findViewById(R.id.rlFavoriteGroups); mRlFavoriteGroups.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { closeSubMenu(); mLvGroupCategories.setVisibility(View.GONE); mTvNoGroups.setVisibility(View.GONE); mTvTitle.setText(getString(R.string.FAVORITES)); if (MediamApp.hasNetworkConnection()) { new GetGroupsAsync(GroupsActivity.this).execute(FAVORITES); } mRlSearchGroups.setVisibility(View.GONE); } }); mRlSearch = (RelativeLayout) findViewById(R.id.rlSearch); mRlSearch.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { closeSubMenu(); mLvGroupCategories.setVisibility(View.GONE); mRlSearchGroups.setVisibility(View.VISIBLE); mTvTitle.setText(getString(R.string.GROUPS)); clearListView(); } }); mRlCreateGroup = (RelativeLayout) findViewById(R.id.rlCreate); mRlCreateGroup.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { closeSubMenu(); Intent intent = new Intent(GroupsActivity.this, CreateGroupActivity.class); GroupsActivity.this.startActivity(intent); flagToCreateGroup = true; } }); mRlCategories = (RelativeLayout) findViewById(R.id.rlCategories); mRlCategories.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { closeSubMenu(); mTvTitle.setText(getString(R.string.CATEGORIES)); mTvNoGroups.setVisibility(View.GONE); mRlSearchGroups.setVisibility(View.GONE); new GetGroupCategoriesAsync(GroupsActivity.this).execute(); } }); mTvNoGroups = (TextView) findViewById(R.id.tvNoGroups); mTvNoGroups.setVisibility(View.GONE); mTvTitle.setText(getString(R.string.FAVORITE_GROUPS)); if (MediamApp.hasNetworkConnection()) { new GetGroupsAsync(GroupsActivity.this).execute(FAVORITES); } mRlSearchGroups.setVisibility(View.GONE); } private void clearListView() { mGroupListAdapter = new GroupsAdapter(GroupsActivity.this, new ArrayList<Center>(), new ArrayList<Notification>()); mLvGroups.setAdapter(mGroupListAdapter); } @Override public void onBackPressed() { if (mRlSearchGroups.getVisibility() == View.VISIBLE) { mRlSearchGroups.setVisibility(View.GONE); if (MediamApp.hasNetworkConnection()) { new GetGroupsAsync(GroupsActivity.this).execute(FAVORITES); } } else { super.onBackPressed(); } } private void OnClickListeners() { mBtnSearchGroups.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { CenterSearch groupSearch = new CenterSearch(); groupSearch.setName(mEtSearchGroups.getText().toString()); new SearchGroupsAsync(GroupsActivity.this).execute(groupSearch); } }); mEtSearchGroups.setOnEditorActionListener(new OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_SEARCH) { CenterSearch groupSearch = new CenterSearch(); groupSearch.setName(mEtSearchGroups.getText().toString()); new SearchGroupsAsync(GroupsActivity.this).execute(groupSearch); return true; } return false; } }); } public void getGroupsForCategory(CenterCategory groupCategory) { new GetGroupsAsync(GroupsActivity.this).execute(CATEGORY, groupCategory.getId()); mTvTitle.setText(groupCategory.getTitle().toUpperCase()); } private static final String ALL_GROUPS = "all_groups"; private static final String FAVORITES = "favorites"; private static final String CATEGORY = "category"; private class GetGroupsAsync extends mediamAsync<String, Void, List<Center>> { String searchType = ""; protected GetGroupsAsync(Context context) { super(context); } HookUpProgressDialog mProgressDialog = new HookUpProgressDialog(GroupsActivity.this); @Override protected void onPreExecute() { super.onPreExecute(); mProgressDialog.show(); } @Override protected List<Center> backgroundWork(String... params) throws ClientProtocolException, JSONException, IOException, mediamException, IllegalStateException, hlmediaForbiddenException { searchType = params[0]; if (UsersManagement.getLoginUser().getActivitySummary() != null) { for (RecentActivity recentActivity : UsersManagement.getLoginUser().getActivitySummary() .getRecentActivityList()) { if (recentActivity.getTargetType().equals(Const.GROUP)) { mGroupNotifications = recentActivity.getNotifications(); } } } if (params[0].equals(ALL_GROUPS)) { return CouchDB.findAllGroups(); } else if (params[0].equals(FAVORITES)) { return CouchDB.findUserFavoriteGroups(UsersManagement.getLoginUser().getId()); } else if (params[0].equals(CATEGORY)) { return CouchDB.findGroupByCategoryId(params[1]); } return CouchDB.findAllGroups(); } @Override protected void onPostExecute(List<Center> result) { super.onPostExecute(result); if (result != null) { mGroups = (ArrayList<Center>) result; if (searchType.equals(FAVORITES) && (result == null || result.size() == 0)) { GroupsActivity.this.showTutorialOnceAfterBoot(getString(R.string.tutorial_nofavorite)); } mLvGroupCategories.setVisibility(View.GONE); mLvGroups.setVisibility(View.VISIBLE); if (mGroups.size() == 0) { mTvNoGroups.setVisibility(View.VISIBLE); mTvNoGroups.setText(getString(R.string.no_groups_in_favorites)); } else { mTvNoGroups.setVisibility(View.GONE); } // sorting groups by name Collections.sort(mGroups, new Comparator<Center>() { @Override public int compare(Center lhs, Center rhs) { return lhs.getName().compareToIgnoreCase(rhs.getName()); } }); if (mGroupListAdapter == null) { mGroupListAdapter = new GroupsAdapter(GroupsActivity.this, mGroups, mGroupNotifications); mLvGroups.setAdapter(mGroupListAdapter); mLvGroups.setOnItemClickListener(mGroupListAdapter); } else { mGroupListAdapter.setItems(mGroups, mGroupNotifications); } } mProgressDialog.dismiss(); } } private class SearchGroupsAsync extends mediamAsync<CenterSearch, Void, List<Center>> { protected SearchGroupsAsync(Context context) { super(context); } HookUpProgressDialog mProgressDialog = new HookUpProgressDialog(GroupsActivity.this); @Override protected void onPreExecute() { super.onPreExecute(); mProgressDialog.show(); } @Override protected List<Center> backgroundWork(CenterSearch... params) throws ClientProtocolException, IOException, JSONException, mediamException, IllegalStateException, hlmediaForbiddenException { return CouchDB.searchGroups(params[0]); } @Override protected void onPostExecute(List<Center> result) { super.onPostExecute(result); if (result != null) { mGroups = new ArrayList<Center>(result); } else { mGroups = new ArrayList<Center>(); } // mGroups = (ArrayList<Group>) result; mLvGroupCategories.setVisibility(View.GONE); mLvGroups.setVisibility(View.VISIBLE); if (mGroups.size() == 0) { mTvNoGroups.setVisibility(View.VISIBLE); mTvNoGroups.setText(getString(R.string.no_groups_found)); } else { mTvNoGroups.setVisibility(View.GONE); } // sorting groups by name Collections.sort(mGroups, new Comparator<Center>() { @Override public int compare(Center lhs, Center rhs) { return lhs.getName().compareToIgnoreCase(rhs.getName()); } }); if (mGroupListAdapter == null) { mGroupListAdapter = new GroupsAdapter(GroupsActivity.this, mGroups, mGroupNotifications); mLvGroups.setAdapter(mGroupListAdapter); mLvGroups.setOnItemClickListener(mGroupListAdapter); } else { mGroupListAdapter.setItems(mGroups, mGroupNotifications); } mProgressDialog.dismiss(); } } private class GetGroupCategoriesAsync extends mediamAsync<CenterSearch, Void, List<CenterCategory>> { protected GetGroupCategoriesAsync(Context context) { super(context); } HookUpProgressDialog mProgressDialog = new HookUpProgressDialog(GroupsActivity.this); @Override protected void onPreExecute() { super.onPreExecute(); mProgressDialog.show(); } @Override protected List<CenterCategory> backgroundWork(CenterSearch... params) throws ClientProtocolException, IOException, JSONException, mediamException, IllegalStateException, hlmediaForbiddenException { return CouchDB.findGroupCategories(); } @Override protected void onPostExecute(List<CenterCategory> result) { super.onPostExecute(result); mGroupCategories = (ArrayList<CenterCategory>) result; mLvGroupCategories.setVisibility(View.VISIBLE); mLvGroups.setVisibility(View.GONE); if (mGroupCategoriesAdapter == null) { mGroupCategoriesAdapter = new GroupCategoriesAdapter(GroupsActivity.this, mGroupCategories); mLvGroupCategories.setAdapter(mGroupCategoriesAdapter); mLvGroupCategories.setOnItemClickListener(mGroupCategoriesAdapter); } else { mGroupCategoriesAdapter.setItems(mGroupCategories); } mProgressDialog.dismiss(); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); // Check which request we're responding to if (requestCode == REQUEST_UPDATE_GROUPS) { // Make sure the request was successful if (resultCode == RESULT_OK) { mLvGroupCategories.setVisibility(View.GONE); mTvNoGroups.setVisibility(View.GONE); mTvTitle.setText(getString(R.string.FAVORITES)); if (MediamApp.hasNetworkConnection()) { new GetGroupsAsync(GroupsActivity.this).execute(FAVORITES); } mRlSearchGroups.setVisibility(View.GONE); } } } }