[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for bug #403 #4

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Do camera availability check recursively based on API level
  • Loading branch information
ozbek committed Apr 25, 2013
commit 25d1be4234edb15531ef6f50ce4139d1c4e6edbd
94 changes: 50 additions & 44 deletions src/org/wordpress/android/ui/WPActionBarActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
Expand Down Expand Up @@ -69,7 +70,7 @@ public abstract class WPActionBarActivity extends SherlockFragmentActivity {
* Request code for reloading menu after returning from the PreferencesActivity.
*/
static final int SETTINGS_REQUEST = 200;

/**
* Used to restore active activity on app creation
*/
Expand All @@ -83,7 +84,7 @@ public abstract class WPActionBarActivity extends SherlockFragmentActivity {
protected static final int VIEW_SITE_ACTIVITY = 7;
protected static final int DASHBOARD_ACTIVITY = 8;
protected static final int SETTINGS_ACTIVITY = 9;

protected MenuDrawer mMenuDrawer;
private static int[] blogIDs;
protected boolean isAnimatingRefreshButton;
Expand All @@ -101,9 +102,9 @@ public abstract class WPActionBarActivity extends SherlockFragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == 4)
mIsXLargeDevice = true;
mIsXLargeDevice = true;
}

@Override
Expand Down Expand Up @@ -144,32 +145,32 @@ protected void onResume() {

/**
* Create a menu drawer and attach it to the activity.
*
*
* @param contentViewID {@link View} of the main content for the activity.
*/
protected void createMenuDrawer(int contentViewID) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

mMenuDrawer = attachMenuDrawer();
mMenuDrawer.setContentView(contentViewID);

initMenuDrawer();
}

/**
* Create a menu drawer and attach it to the activity.
*
*
* @param contentView {@link View} of the main content for the activity.
*/
protected void createMenuDrawer(View contentView) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

mMenuDrawer = attachMenuDrawer();
mMenuDrawer.setContentView(contentView);

initMenuDrawer();
}

/**
* Attach a menu drawer to the Activity
* Set to be a static drawer if on a landscape x-large device
Expand All @@ -193,7 +194,7 @@ private MenuDrawer attachMenuDrawer() {
menuDrawer.setDropShadowColor(getResources().getColor(R.color.md__shadowColor));
return menuDrawer;
}

/**
* Create menu drawer ListView and listeners
*/
Expand All @@ -203,12 +204,12 @@ private void initMenuDrawer() {
mListView.setDivider(null);
mListView.setDividerHeight(0);
mListView.setCacheColorHint(android.R.color.transparent);

String[] blogNames = getBlogNames();
if (blogNames.length > 1) {
addBlogSpinner(blogNames);
}

mListView.setOnItemClickListener(mItemClickListener);
mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
Expand All @@ -221,7 +222,7 @@ public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCoun
mMenuDrawer.invalidate();
}
});

mMenuDrawer.setMenuView(mListView);

updateMenuDrawer();
Expand All @@ -230,7 +231,7 @@ public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCoun
private void addBlogSpinner(String[] blogNames) {
LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout spinnerWrapper = (LinearLayout) layoutInflater.inflate(R.layout.blog_spinner, null);

spinnerWrapper.setOnClickListener(new OnClickListener() {

@Override
Expand All @@ -240,7 +241,7 @@ public void onClick(View v) {
}
}
});

mBlogSpinner = (IcsSpinner) spinnerWrapper.findViewById(R.id.blog_spinner);
mBlogSpinner.setOnItemSelectedListener(mItemSelectedListener);
SpinnerAdapter mSpinnerAdapter = new ArrayAdapter<String>(getSupportActionBar()
Expand Down Expand Up @@ -300,7 +301,7 @@ protected void updateMenuDrawer() {

if ((WPActionBarActivity.this instanceof ReaderActivity))
mActivePosition = 0;

if ((WPActionBarActivity.this instanceof PostsActivity))
mActivePosition = 1;

Expand All @@ -314,7 +315,7 @@ else if ((WPActionBarActivity.this instanceof ViewSiteActivity))
mActivePosition = 7;
else if ((WPActionBarActivity.this instanceof DashboardActivity))
mActivePosition = 8;

mAdapter = new MenuAdapter(items);
mListView.setAdapter(mAdapter);
}
Expand All @@ -325,16 +326,16 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
// Adjust position if only one blog is in the app
if (mListView.getHeaderViewsCount() > 0 && position > 0)
position--;

if (!mIsDotComBlog)
position++;

if (position == mActivePosition) {
// Same row selected
mMenuDrawer.closeMenu();
return;
}

int activityTag = (Integer) view.getTag();

mActivePosition = position;
Expand All @@ -343,7 +344,7 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(WPActionBarActivity.this);
SharedPreferences.Editor editor = settings.edit();

switch (activityTag) {
case READER_ACTIVITY:
if (!(WPActionBarActivity.this instanceof ReaderActivity))
Expand Down Expand Up @@ -399,25 +400,16 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
break;
case QUICK_PHOTO_ACTIVITY:
mShouldFinish = false;
PackageManager pm = WPActionBarActivity.this.getPackageManager();
intent = new Intent(WPActionBarActivity.this, EditPostActivity.class);
if
(pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
intent.putExtra("quick-media", Constants.QUICK_POST_PHOTO_CAMERA);
} else {
intent.putExtra("quick-media", Constants.QUICK_POST_PHOTO_LIBRARY);
}
intent.putExtra("quick-media", hasCamera() ? Constants.QUICK_POST_PHOTO_CAMERA
: Constants.QUICK_POST_PHOTO_LIBRARY);
intent.putExtra("isNew", true);
break;
case QUICK_VIDEO_ACTIVITY:
mShouldFinish = false;
PackageManager vpm = WPActionBarActivity.this.getPackageManager();
intent = new Intent(WPActionBarActivity.this, EditPostActivity.class);
if (vpm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
intent.putExtra("quick-media", Constants.QUICK_POST_VIDEO_CAMERA);
} else {
intent.putExtra("quick-media", Constants.QUICK_POST_VIDEO_LIBRARY);
}
intent.putExtra("quick-media", hasCamera() ? Constants.QUICK_POST_VIDEO_CAMERA
: Constants.QUICK_POST_VIDEO_LIBRARY);
intent.putExtra("isNew", true);
break;
case VIEW_SITE_ACTIVITY:
Expand Down Expand Up @@ -450,7 +442,21 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
}
}
};


private boolean hasCamera() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is created twice. In order to prevent DRY how about you move it to one of the utility classes? Maybe DeviceUtils?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I also thought about that and looked through utility classes. None looked to fit.
DeviceUtils was the first choice, but it appears to be for wider range: Android vs BB vs Kindle...

If you think puting this to DeviceUtils is fine, then I can do it.
Please confirm.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think it's the best place for it even though it is currently used for higher level stuff. Go ahead and put it there. Thanks for your hard work! I've tested it out on 2.2, 2.3 and 4.1 devices and it works fine.

PackageManager pm = getPackageManager();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) {
return pm.hasSystemFeature(PackageManager.FEATURE_CAMERA);
}

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
return pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)
|| pm.hasSystemFeature("android.hardware.camera.front"/*PackageManager.FEATURE_CAMERA_FRONT*/);
}

return pm.hasSystemFeature("android.hardware.camera.any"/*PackageManager.FEATURE_CAMERA_ANY*/);
}

private class MenuAdapter extends BaseAdapter {

private List<Object> mItems;
Expand Down Expand Up @@ -498,7 +504,7 @@ public View getView(int position, View convertView, ViewGroup parent) {

ImageView iconImageView = (ImageView) v.findViewById(R.id.menu_row_icon);
iconImageView.setImageResource(((MenuDrawerItem) item).mIconRes);

v.setTag((mIsDotComBlog) ? position : position + 1);

int positionCheck = mActivePosition;
Expand All @@ -518,7 +524,7 @@ public View getView(int position, View convertView, ViewGroup parent) {


TextView bagdeTextView = (TextView) v.findViewById(R.id.menu_row_badge);
int commentRow = (mIsDotComBlog) ? 3 : 2;
int commentRow = (mIsDotComBlog) ? 3 : 2;
if (position == commentRow && WordPress.currentBlog != null) {
int commentCount = WordPress.currentBlog.getUnmoderatedCommentCount();
if (commentCount > 0) {
Expand Down Expand Up @@ -569,7 +575,7 @@ public void onBackPressed() {
/**
* Get the names of all the blogs configured within the application. If a
* blog does not have a specific name, the blog URL is returned.
*
*
* @return array of blog names
*/
private static String[] getBlogNames() {
Expand Down Expand Up @@ -650,7 +656,7 @@ else if (blogNames.length > 1 && mBlogSpinner != null) {
R.layout.sherlock_spinner_dropdown_item, blogNames);
mBlogSpinner.setAdapter(mSpinnerAdapter);
}

if (blogNames.length >= 1) {
setupCurrentBlog();
onBlogChanged();
Expand All @@ -660,7 +666,7 @@ else if (blogNames.length > 1 && mBlogSpinner != null) {
break;
}
}

private IcsAdapterView.OnItemSelectedListener mItemSelectedListener = new IcsAdapterView.OnItemSelectedListener() {

@Override
Expand All @@ -676,7 +682,7 @@ public void onItemSelected(IcsAdapterView<?> arg0, View arg1, int position, long
}

@Override
public void onNothingSelected(IcsAdapterView<?> arg0) {
public void onNothingSelected(IcsAdapterView<?> arg0) {
}
};

Expand Down Expand Up @@ -721,10 +727,10 @@ public void stopAnimatingRefreshButton(MenuItem refreshItem) {
refreshItem.setActionView(null);
}
}

@Override
public void onConfigurationChanged(Configuration newConfig) {

if (mIsXLargeDevice) {
// Re-attach the drawer if an XLarge device is rotated, so it can be static if in landscape
View content = mMenuDrawer.getContentContainer().getChildAt(0);
Expand All @@ -733,7 +739,7 @@ public void onConfigurationChanged(Configuration newConfig) {
mMenuDrawer.setContentView(content);
initMenuDrawer();
}

super.onConfigurationChanged(newConfig);
}
}
Loading