Skip to content

Commit

Permalink
Do camera availability check recursively based on API level
Browse files Browse the repository at this point in the history
  • Loading branch information
ozbek committed Apr 25, 2013
1 parent 2eedabe commit 25d1be4
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 56 deletions.
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() {
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

0 comments on commit 25d1be4

Please sign in to comment.