From: gumartinm Date: Thu, 21 Jun 2012 08:22:43 +0000 (+0200) Subject: Trying to custom my action bar after logged in. X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=882e4f5c348bed450bccaf3ab77a936ea9ce8c7c;p=JavaForFun Trying to custom my action bar after logged in. WIP --- diff --git a/Android/MobiAdsReloaded/AndroidManifest.xml b/Android/MobiAdsReloaded/AndroidManifest.xml index 8140b7f..8a7185f 100644 --- a/Android/MobiAdsReloaded/AndroidManifest.xml +++ b/Android/MobiAdsReloaded/AndroidManifest.xml @@ -30,14 +30,30 @@ android:theme="@android:style/Theme.Holo" android:screenOrientation="portrait" android:configChanges="touchscreen|keyboard" - android:launchMode="standard" - android:uiOptions="splitActionBarWhenNarrow" > + android:uiOptions="splitActionBarWhenNarrow" + android:launchMode="standard"> + + + + + + + + + - - - - - - - - - - - + android:checkable="false" + android:enabled="false" + android:showAsAction="ifRoom|withText"/> \ No newline at end of file diff --git a/Android/MobiAdsReloaded/res/menu/selectedmenuads.xml b/Android/MobiAdsReloaded/res/menu/selectedmenuads.xml index e9c31cf..72d3cf7 100644 --- a/Android/MobiAdsReloaded/res/menu/selectedmenuads.xml +++ b/Android/MobiAdsReloaded/res/menu/selectedmenuads.xml @@ -1,7 +1,7 @@ - UTF-8 description Remove + Settings + Login You have no downloaded ads. \ No newline at end of file diff --git a/Android/MobiAdsReloaded/src/de/android/mobiads/MobiAdsLoginActivity.java b/Android/MobiAdsReloaded/src/de/android/mobiads/MobiAdsLoginActivity.java index 5000294..f4b617d 100644 --- a/Android/MobiAdsReloaded/src/de/android/mobiads/MobiAdsLoginActivity.java +++ b/Android/MobiAdsReloaded/src/de/android/mobiads/MobiAdsLoginActivity.java @@ -52,67 +52,65 @@ public class MobiAdsLoginActivity extends Activity { HttpEntity httpEntity = null; HttpResponse httpResponse = null; final List formParams = new ArrayList(2); - - Cookie.setCookie("prueba"); - -// httpClient.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, getResources().getString(R.string.encoded_web_service)); -// httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, getResources().getString(R.string.user_agent_web_service)); -// httpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); -// //TODO: RESTful Web Service must use JSON instead of signin array :( -// formParams.add(new BasicNameValuePair("signin[username]", username.getText().toString())); -// formParams.add(new BasicNameValuePair("signin[password]", password.getText().toString())); -// try { -// httpEntity = new UrlEncodedFormEntity(formParams, getResources().getString(R.string.encoded_web_service)); -// httpPost.setEntity(httpEntity); -// httpResponse = httpClient.execute(httpPost); -// } catch (UnsupportedEncodingException e) { -// Log.e(TAG, "Error while encoding POST parameters.", e); -// return; -// } catch (ClientProtocolException e) { -// Log.e(TAG, "Error while executing HTTP client connection.", e); -// createErrorDialog(R.string.error_dialog_connection_error); -// return; -// } catch (IOException e) { -// Log.e(TAG, "Error while executing HTTP client connection.", e); -// createErrorDialog(R.string.error_dialog_connection_error); -// return; -// } finally { -// httpClient.getConnectionManager().shutdown(); -// } -// -// if (httpResponse != null) { -// switch (httpResponse.getStatusLine().getStatusCode()) { -// case HttpStatus.SC_OK: -// String cookie = httpResponse.getLastHeader(SETCOOKIEFIELD).getValue(); -// if (cookie != null) { -// cookie = cookie.split(";")[0]; -// //Go to the next activity -// StrictMode.setThreadPolicy(currentPolicy); -// Cookie.setCookie(cookie); -// this.finish(); -// } else { -// Log.e(TAG, "There must be a weird issue with the server because... There is not cookie!!!!"); -// createErrorDialog(R.string.error_dialog_connection_error); -// } -// break; -// case HttpStatus.SC_UNAUTHORIZED: -// //Username or password is incorrect -// createErrorDialog(R.string.error_dialog_userpwd_error); -// break; -// case HttpStatus.SC_BAD_REQUEST: -// //What the heck are you doing? -// createErrorDialog(R.string.error_dialog_userpwd_error); -// break; -// default: -// Log.e(TAG, "Error while retrieving the HTTP status line."); -// createErrorDialog(R.string.error_dialog_connection_error); -// break; -// } -// } -// else { -// Log.e(TAG, "No response? This should never have happened."); -// createErrorDialog(R.string.error_dialog_connection_error); -// } + + httpClient.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, getResources().getString(R.string.encoded_web_service)); + httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, getResources().getString(R.string.user_agent_web_service)); + httpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); + //TODO: RESTful Web Service must use JSON instead of signin array :( + formParams.add(new BasicNameValuePair("signin[username]", username.getText().toString())); + formParams.add(new BasicNameValuePair("signin[password]", password.getText().toString())); + try { + httpEntity = new UrlEncodedFormEntity(formParams, getResources().getString(R.string.encoded_web_service)); + httpPost.setEntity(httpEntity); + httpResponse = httpClient.execute(httpPost); + } catch (UnsupportedEncodingException e) { + Log.e(TAG, "Error while encoding POST parameters.", e); + return; + } catch (ClientProtocolException e) { + Log.e(TAG, "Error while executing HTTP client connection.", e); + createErrorDialog(R.string.error_dialog_connection_error); + return; + } catch (IOException e) { + Log.e(TAG, "Error while executing HTTP client connection.", e); + createErrorDialog(R.string.error_dialog_connection_error); + return; + } finally { + httpClient.getConnectionManager().shutdown(); + } + + if (httpResponse != null) { + switch (httpResponse.getStatusLine().getStatusCode()) { + case HttpStatus.SC_OK: + String cookie = httpResponse.getLastHeader(SETCOOKIEFIELD).getValue(); + if (cookie != null) { + cookie = cookie.split(";")[0]; + //Go to the next activity + StrictMode.setThreadPolicy(currentPolicy); + Cookie.setCookie(cookie); + this.finish(); + } else { + Log.e(TAG, "There must be a weird issue with the server because... There is not cookie!!!!"); + createErrorDialog(R.string.error_dialog_connection_error); + } + break; + case HttpStatus.SC_UNAUTHORIZED: + //Username or password is incorrect + createErrorDialog(R.string.error_dialog_userpwd_error); + break; + case HttpStatus.SC_BAD_REQUEST: + //What the heck are you doing? + createErrorDialog(R.string.error_dialog_userpwd_error); + break; + default: + Log.e(TAG, "Error while retrieving the HTTP status line."); + createErrorDialog(R.string.error_dialog_connection_error); + break; + } + } + else { + Log.e(TAG, "No response? This should never have happened."); + createErrorDialog(R.string.error_dialog_connection_error); + } } public void onClickLocalAds(View v) { diff --git a/Android/MobiAdsReloaded/src/de/android/mobiads/MobiAdsPreferences.java b/Android/MobiAdsReloaded/src/de/android/mobiads/MobiAdsPreferences.java index e717474..a999c4d 100644 --- a/Android/MobiAdsReloaded/src/de/android/mobiads/MobiAdsPreferences.java +++ b/Android/MobiAdsReloaded/src/de/android/mobiads/MobiAdsPreferences.java @@ -1,55 +1,76 @@ package de.android.mobiads; +import android.app.Activity; import android.app.ActivityManager; import android.app.ActivityManager.RunningServiceInfo; +import android.app.FragmentManager; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.preference.CheckBoxPreference; import android.preference.Preference; import android.preference.PreferenceFragment; +import de.android.mobiads.list.MobiAdsList.MobiAdsListFragment; -public class MobiAdsPreferences extends PreferenceFragment { +public class MobiAdsPreferences extends Activity { @Override - public void onCreate(Bundle savedInstanceState) { + protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - // Load the preferences from an XML resource - addPreferencesFromResource(R.xml.preferences); + FragmentManager fm = getFragmentManager(); + + // Create the list fragment and add it as our sole content. + if (fm.findFragmentById(android.R.id.content) == null) { + MobiAdsListFragment list = new MobiAdsListFragment(); + fm.beginTransaction().add(android.R.id.content, list).commit(); + } + + } - @Override - public void onActivityCreated(Bundle savedInstanceState) { - super.onActivityCreated(savedInstanceState); + public static class MobiAdsPreferencesFragment extends PreferenceFragment { + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + // Load the preferences from an XML resource + addPreferencesFromResource(R.xml.preferences); + } + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); - CheckBoxPreference checkBoxPreference = (CheckBoxPreference) findPreference("service_started"); - checkBoxPreference.setChecked(isMyServiceRunning()); - checkBoxPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { + CheckBoxPreference checkBoxPreference = (CheckBoxPreference) findPreference("service_started"); + checkBoxPreference.setChecked(isMyServiceRunning()); + checkBoxPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { - @Override - public boolean onPreferenceClick(Preference preference) { - CheckBoxPreference checkBoxPreference = (CheckBoxPreference) preference; - if (checkBoxPreference.isChecked()) { - Intent intent = new Intent(getActivity(), MobiAdsService.class); - intent.putExtra("cookie", Cookie.getCookie()); - getActivity().startService(intent); + @Override + public boolean onPreferenceClick(Preference preference) { + CheckBoxPreference checkBoxPreference = (CheckBoxPreference) preference; + if (checkBoxPreference.isChecked()) { + Intent intent = new Intent(getActivity(), MobiAdsService.class); + intent.putExtra("cookie", Cookie.getCookie()); + getActivity().startService(intent); + } + else { + getActivity().stopService(new Intent(getActivity(), MobiAdsService.class)); + } + return false; } - else { - getActivity().stopService(new Intent(getActivity(), MobiAdsService.class)); + }); + } + + private boolean isMyServiceRunning() { + ActivityManager manager = (ActivityManager) getActivity().getSystemService(Context.ACTIVITY_SERVICE); + for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { + if (MobiAdsService.class.getName().equals(service.service.getClassName())) { + return true; } - return false; } - }); - } - - private boolean isMyServiceRunning() { - ActivityManager manager = (ActivityManager) getActivity().getSystemService(Context.ACTIVITY_SERVICE); - for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { - if (MobiAdsService.class.getName().equals(service.service.getClassName())) { - return true; - } - } - return false; - } + return false; + } + } } diff --git a/Android/MobiAdsReloaded/src/de/android/mobiads/MobiAdsSettings.java b/Android/MobiAdsReloaded/src/de/android/mobiads/MobiAdsSettings.java new file mode 100644 index 0000000..edaa4bb --- /dev/null +++ b/Android/MobiAdsReloaded/src/de/android/mobiads/MobiAdsSettings.java @@ -0,0 +1,75 @@ +package de.android.mobiads; + +import android.app.Activity; +import android.app.ActivityManager; +import android.app.ActivityManager.RunningServiceInfo; +import android.app.FragmentManager; +import android.content.Context; +import android.content.Intent; +import android.os.Bundle; +import android.preference.CheckBoxPreference; +import android.preference.Preference; +import android.preference.PreferenceFragment; + +public class MobiAdsSettings extends Activity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + FragmentManager fm = getFragmentManager(); + + // Create the list fragment and add it as our sole content. + if (fm.findFragmentById(android.R.id.content) == null) { + MobiAdsSettingsFragment list = new MobiAdsSettingsFragment(); + fm.beginTransaction().add(android.R.id.content, list).commit(); + } + + + } + + public static class MobiAdsSettingsFragment extends PreferenceFragment { + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + // Load the preferences from an XML resource + addPreferencesFromResource(R.xml.preferences); + } + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + + CheckBoxPreference checkBoxPreference = (CheckBoxPreference) findPreference("service_started"); + checkBoxPreference.setChecked(isMyServiceRunning()); + checkBoxPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { + + @Override + public boolean onPreferenceClick(Preference preference) { + CheckBoxPreference checkBoxPreference = (CheckBoxPreference) preference; + if (checkBoxPreference.isChecked()) { + Intent intent = new Intent(getActivity(), MobiAdsService.class); + intent.putExtra("cookie", Cookie.getCookie()); + getActivity().startService(intent); + } + else { + getActivity().stopService(new Intent(getActivity(), MobiAdsService.class)); + } + return false; + } + }); + } + + private boolean isMyServiceRunning() { + ActivityManager manager = (ActivityManager) getActivity().getSystemService(Context.ACTIVITY_SERVICE); + for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { + if (MobiAdsService.class.getName().equals(service.service.getClassName())) { + return true; + } + } + return false; + } + } +} diff --git a/Android/MobiAdsReloaded/src/de/android/mobiads/MobiAdsTabsActivity.java b/Android/MobiAdsReloaded/src/de/android/mobiads/MobiAdsTabsActivity.java index d40cdec..fb3c27b 100644 --- a/Android/MobiAdsReloaded/src/de/android/mobiads/MobiAdsTabsActivity.java +++ b/Android/MobiAdsReloaded/src/de/android/mobiads/MobiAdsTabsActivity.java @@ -36,8 +36,8 @@ public class MobiAdsTabsActivity extends Activity { if (Cookie.getCookie() != null || isMyServiceRunning()) { bar.addTab(bar.newTab() .setText("Control Panel") - .setTabListener(new TabListener( - this, "controlpanel", MobiAdsPreferences.class))); + .setTabListener(new TabListener( + this, "controlpanel", MobiAdsPreferences.MobiAdsPreferencesFragment.class))); } bar.addTab(bar.newTab() @@ -63,8 +63,8 @@ public class MobiAdsTabsActivity extends Activity { if ((Cookie.getCookie() != null) && (getActionBar().getTabCount() == 2)) { getActionBar().addTab(getActionBar().newTab() .setText("Control Panel") - .setTabListener(new TabListener( - this, "controlpanel", MobiAdsPreferences.class)), 1); + .setTabListener(new TabListener( + this, "controlpanel", MobiAdsPreferences.MobiAdsPreferencesFragment.class)), 1); } } diff --git a/Android/MobiAdsReloaded/src/de/android/mobiads/list/MobiAdsLatest.java b/Android/MobiAdsReloaded/src/de/android/mobiads/list/MobiAdsLatest.java index 767a3e7..8160366 100644 --- a/Android/MobiAdsReloaded/src/de/android/mobiads/list/MobiAdsLatest.java +++ b/Android/MobiAdsReloaded/src/de/android/mobiads/list/MobiAdsLatest.java @@ -94,7 +94,7 @@ public class MobiAdsLatest extends Activity { public boolean onActionItemClicked(ActionMode mode, MenuItem item) { // Respond to clicks on the actions in the CAB switch (item.getItemId()) { - case R.id.menuadsremove: + case R.id.selectedmenu_remove: SparseBooleanArray itemsPositions = getListView().getCheckedItemPositions(); Collection aux = new ArrayList(mAdapter.getCount()); for (int i=0; i< itemsPositions.size(); i++) { diff --git a/Android/MobiAdsReloaded/src/de/android/mobiads/list/MobiAdsLatestList.java b/Android/MobiAdsReloaded/src/de/android/mobiads/list/MobiAdsLatestList.java index 3796d71..6fad3e0 100644 --- a/Android/MobiAdsReloaded/src/de/android/mobiads/list/MobiAdsLatestList.java +++ b/Android/MobiAdsReloaded/src/de/android/mobiads/list/MobiAdsLatestList.java @@ -95,7 +95,7 @@ public class MobiAdsLatestList extends ListActivity implements LoaderManager.Loa public boolean onContextItemSelected(MenuItem item) { AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); switch (item.getItemId()) { - case R.id.menuadsremove: + case R.id.selectedmenu_remove: removeAd(info.position); return true; default: diff --git a/Android/MobiAdsReloaded/src/de/android/mobiads/list/MobiAdsList.java b/Android/MobiAdsReloaded/src/de/android/mobiads/list/MobiAdsList.java index a33a35e..9b0b868 100644 --- a/Android/MobiAdsReloaded/src/de/android/mobiads/list/MobiAdsList.java +++ b/Android/MobiAdsReloaded/src/de/android/mobiads/list/MobiAdsList.java @@ -17,6 +17,7 @@ import android.app.LoaderManager; import android.app.Notification; import android.app.NotificationManager; import android.content.AsyncTaskLoader; +import android.content.ComponentName; import android.content.ContentValues; import android.content.Context; import android.content.Intent; @@ -43,21 +44,25 @@ import android.widget.ListView; import android.widget.SearchView; import android.widget.SearchView.OnQueryTextListener; import android.widget.TextView; +import de.android.mobiads.Cookie; import de.android.mobiads.MobiAdsService; import de.android.mobiads.R; import de.android.mobiads.provider.Indexer; public class MobiAdsList extends Activity { + Menu mMenu; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - final ActionBar bar = getActionBar(); + final ActionBar actionBar = getActionBar(); - bar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); - bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE, ActionBar.DISPLAY_SHOW_TITLE); - bar.setTitle(getResources().getString(R.string.header_bar)); + actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); + actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE, ActionBar.DISPLAY_SHOW_TITLE); + actionBar.setTitle(getResources().getString(R.string.header_bar)); + //actionBar.setDisplayHomeAsUpEnabled(true); + FragmentManager fm = getFragmentManager(); @@ -73,12 +78,31 @@ public class MobiAdsList extends Activity { @Override public boolean onOptionsItemSelected(MenuItem item) { - return super.onOptionsItemSelected(item); + super.onOptionsItemSelected(item); + Intent intent = null; + switch (item.getItemId()) { + case R.id.menuads_settings: + intent = new Intent("android.intent.action.MOBIADS"). + setComponent(new ComponentName("de.android.mobiads", "de.android.mobiads.MobiAdsSettings")); + this.startActivity(intent); + return true; + case R.id.menuads_login: + intent = new Intent("android.intent.action.MOBIADS"). + setComponent(new ComponentName("de.android.mobiads", "de.android.mobiads.MobiAdsLoginActivity")); + this.startActivity(intent); + return true; + default: + return false; + } } @Override - public boolean onPrepareOptionsMenu(Menu menu) { - return super.onPrepareOptionsMenu(menu); + public boolean onPrepareOptionsMenu(Menu menu) { + super.onPrepareOptionsMenu(menu); + MenuItem item = menu.findItem(R.id.menuads_settings); + item.getIcon().setAlpha(70); + mMenu = menu; + return true; } @Override @@ -89,6 +113,28 @@ public class MobiAdsList extends Activity { return true; } + + public void onOptionsMenuClosed(Menu menu) { + super.onOptionsMenuClosed(menu); + } + + //Si doy al boton pa tras pasa por onCreateOptionsMenu y onPrepareOptionsMenu + //Si doy al boton home solo pasa por onResume + //:/ + @Override + protected void onResume() { + super.onResume(); + +// if (Cookie.getCookie() != null) { +// MenuItem item = mMenu.findItem(R.id.menuads_settings); +// item.getIcon().setAlpha(255); +// item = mMenu.findItem(R.id.menuads_settings); +// item.setEnabled(true); +// item = mMenu.findItem(R.id.menuads_login); +// item.setEnabled(false); +// item.setVisible(false); +// } + } /** @@ -269,7 +315,7 @@ public class MobiAdsList extends Activity { public boolean onActionItemClicked(ActionMode mode, MenuItem item) { // Respond to clicks on the actions in the CAB switch (item.getItemId()) { - case R.id.selectedmenuads: + case R.id.selectedmenu_remove: SparseBooleanArray itemsPositions = getListView().getCheckedItemPositions(); Collection aux = new ArrayList(mAdapter.getCount()); for (int i=0; i< itemsPositions.size(); i++) { @@ -328,7 +374,7 @@ public class MobiAdsList extends Activity { setEmptyText("No downloaded Ads."); // We have a menu item to show in action bar. - setHasOptionsMenu(false); + setHasOptionsMenu(true); mAdapter = new AdsEntryAdapter(getActivity(), R.layout.ads_entry_list_item); @@ -374,46 +420,17 @@ public class MobiAdsList extends Activity { /** * When setHasOptionsMenu(true) we populate the action bar with this menu. It is merged to the - * current action bar menu (if there is no) + * current action bar menu (if there is no one) */ @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { // Place an action bar item for searching. MenuItem item = menu.add("Search"); item.setIcon(android.R.drawable.ic_menu_search); - item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); + item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); SearchView sv = new SearchView(getActivity()); sv.setOnQueryTextListener(this); item.setActionView(sv); - - item = menu.add("Login"); - item.setIcon(android.R.drawable.ic_lock_power_off); - item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); - - item = menu.add("Login"); - item.setIcon(android.R.drawable.ic_lock_power_off); - item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); - - item = menu.add("Login"); - item.setIcon(android.R.drawable.ic_lock_power_off); - item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); - - item = menu.add("Login"); - item.setIcon(android.R.drawable.ic_lock_power_off); - item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); - - item = menu.add("Login"); - item.setIcon(android.R.drawable.ic_lock_power_off); - item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); - - item = menu.add("Login"); - item.setIcon(android.R.drawable.ic_menu_upload); - item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); - - item = menu.add("Login"); - item.setIcon(android.R.drawable.ic_lock_power_off); - item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); - } @Override