Skip to content

Commit

Permalink
添加Android 6.0的权限支持;支持全屏查看图片
Browse files Browse the repository at this point in the history
  • Loading branch information
IvorHu committed Mar 17, 2016
1 parent 70357c1 commit 347cde9
Show file tree
Hide file tree
Showing 24 changed files with 229 additions and 239 deletions.
2 changes: 1 addition & 1 deletion .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/src/main/java/com/example/ivor_hu/meizhi/APP.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* Created by Ivor on 2016/2/12.
*/
public class APP extends Application {

@Override
public void onCreate() {
super.onCreate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -96,7 +95,7 @@ public void onBindViewHolder(ViewHolder holder, int position) {
((ItemViewHolder) holder).textView.setClickable(false);
}
} else {
((HeaderViewHolder) holder).textView.setText(position == 0 ? R.string.libs_used : R.string.feas_used);
((HeaderViewHolder) holder).textView.setText(position == 0 ? R.string.about_libs_used : R.string.about_feas_used);
}

}
Expand Down Expand Up @@ -138,7 +137,6 @@ public ItemViewHolder(View itemView) {
public void onClick(View v) {
final int pos = getAdapterPosition();
if (pos < mLibsList.size() + 1 && pos != 0) {
Log.d(TAG, "onClick: " + mLibsList.valueAt(pos - 1));
CommonUtil.openUrl(AboutActivity.this, mLibsList.valueAt(pos - 1));
}
}
Expand Down
44 changes: 10 additions & 34 deletions app/src/main/java/com/example/ivor_hu/meizhi/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
Expand All @@ -14,7 +13,6 @@
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuItem;
Expand All @@ -23,34 +21,28 @@

import com.example.ivor_hu.meizhi.utils.CommonUtil;
import com.example.ivor_hu.meizhi.utils.Constants;
import com.example.ivor_hu.meizhi.widget.GirlsFragment;
import com.example.ivor_hu.meizhi.widget.StuffFragment;

import java.util.List;
import java.util.Map;

import static com.example.ivor_hu.meizhi.utils.Constants.TYPES;
import static com.example.ivor_hu.meizhi.utils.Constants.TYPE_ANDROID;
import static com.example.ivor_hu.meizhi.utils.Constants.TYPE_APP;
import static com.example.ivor_hu.meizhi.utils.Constants.TYPE_COLLECTIONS;
import static com.example.ivor_hu.meizhi.utils.Constants.TYPE_FUN;
import static com.example.ivor_hu.meizhi.utils.Constants.TYPE_GIRLS;
import static com.example.ivor_hu.meizhi.utils.Constants.TYPE_IOS;
import static com.example.ivor_hu.meizhi.utils.Constants.TYPE_OTHERS;
import static com.example.ivor_hu.meizhi.utils.Constants.TYPE_WEB;

public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private static final String TAG = "MainActivity";
private static final String CURR_TYPE = "curr_fragment_type";

FloatingActionButton mFab;
private FloatingActionButton mFab;
private Toolbar mToolbar;
private Fragment mCurrFragment;
private String mCurrFragmentType;
GestureDetector mGestureDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onDoubleTap(MotionEvent e) {
Log.d(TAG, "onDoubleTap");
CommonUtil.makeSnackBar(mFab, getResources().getString(R.string.str_double_taps), Snackbar.LENGTH_LONG);
CommonUtil.makeSnackBar(mToolbar, getResources().getString(R.string.main_double_taps), Snackbar.LENGTH_LONG);
return true;
}
});
Expand All @@ -61,7 +53,6 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

CoordinatorLayout mCoordinatorLayout = (CoordinatorLayout) findViewById(R.id.main_coordinator);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
mToolbar.setTitle(R.string.nav_girls);
setSupportActionBar(mToolbar);
Expand Down Expand Up @@ -195,20 +186,9 @@ public boolean onNavigationItemSelected(MenuItem item) {
FragmentManager manager = getSupportFragmentManager();
if (id == R.id.nav_girls) {
swithTo(manager, TYPE_GIRLS, new GirlsFragment());
} else if (id == R.id.nav_android) {
swithTo(manager, TYPE_ANDROID, StuffFragment.newInstance(TYPE_ANDROID));
} else if (id == R.id.nav_ios) {
swithTo(manager, TYPE_IOS, StuffFragment.newInstance(TYPE_IOS));
} else if (id == R.id.nav_web) {
swithTo(manager, TYPE_WEB, StuffFragment.newInstance(TYPE_WEB));
} else if (id == R.id.nav_fun) {
swithTo(manager, TYPE_FUN, StuffFragment.newInstance(TYPE_FUN));
} else if (id == R.id.nav_app) {
swithTo(manager, TYPE_APP, StuffFragment.newInstance(TYPE_APP));
} else if (id == R.id.nav_others) {
swithTo(manager, TYPE_OTHERS, StuffFragment.newInstance(TYPE_OTHERS));
} else if (id == R.id.nav_collections) {
swithTo(manager, TYPE_COLLECTIONS, StuffFragment.newInstance(TYPE_COLLECTIONS));
} else {
String type = Constants.getTypeFromResId(id);
swithTo(manager, type, StuffFragment.newInstance(type));
}
// else if (id == R.id.nav_test) {
// Log.d(TAG, "onNavigationItemSelected: test");
Expand All @@ -222,7 +202,6 @@ public boolean onNavigationItemSelected(MenuItem item) {
private void swithTo(FragmentManager manager, String type, Fragment addedFragment) {
Fragment fragment = manager.findFragmentByTag(type);
if (null != fragment) {
Log.d(TAG, type + " is not null.");
hideAndShow(manager, fragment, type);
} else {
hideAndAdd(manager, addedFragment, type);
Expand All @@ -247,9 +226,8 @@ private void hideAndShow(FragmentManager manager, Fragment newFragment, String f
private void updateLikedData(Fragment newFragment, String fragmentIdx) {
if (fragmentIdx.equals(TYPE_GIRLS)) {
return;
} else {
((StuffFragment) newFragment).updateData();
}
((StuffFragment) newFragment).updateData();
}

@Override
Expand All @@ -262,16 +240,14 @@ public void onActivityReenter(int resultCode, Intent data) {
reenterState = new Bundle(data.getExtras());

final int index = reenterState.getInt("index", 0);
// Log.d(TAG, "onActivityReenter: " + index);
((GirlsFragment) mCurrFragment).onActivityReenter(index);
}
}

// public void hideFab() {
// if (mFab != null && isFabShown) {
//// FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mFab.getLayoutParams();
//// mFab.animate().translationY(mFab.getHeight() + lp.bottomMargin).setInterpolator(new AccelerateInterpolator(2));
// mFab.animate().translationY(screenHeight - mFab.getY()).setInterpolator(new AccelerateInterpolator(2));
// CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) mFab.getLayoutParams();
// mFab.animate().translationY(mFab.getHeight() + lp.bottomMargin).setInterpolator(new AccelerateInterpolator(2));
// isFabShown = false;
// }
// }
Expand Down
77 changes: 42 additions & 35 deletions app/src/main/java/com/example/ivor_hu/meizhi/ViewerActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
Expand All @@ -13,29 +12,23 @@
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.NavUtils;
import android.support.v4.app.SharedElementCallback;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.transition.Explode;
import android.transition.Fade;
import android.transition.Slide;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.Toast;
import android.widget.FrameLayout;

import com.example.ivor_hu.meizhi.db.Image;
import com.example.ivor_hu.meizhi.utils.CommonUtil;
import com.example.ivor_hu.meizhi.utils.Constants;
import com.example.ivor_hu.meizhi.utils.PicUtil;
import com.example.ivor_hu.meizhi.widget.GirlsFragment;
import com.example.ivor_hu.meizhi.widget.ViewerFragment;

import java.io.File;
import java.util.List;
Expand All @@ -50,14 +43,13 @@
*/
public class ViewerActivity extends AppCompatActivity {
public static final String TAG = "ViewerActivity";

private static final int WRITE_EXTERNAL_STORAGE_REQUEST_CODE = 111;
private final Handler msgHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.arg1) {
case PicUtil.SAVE_DONE_TOAST:
String filepath = msg.getData().getString(PicUtil.FILEPATH);
CommonUtil.makeSnackBar(mViewPager, "已保存至" + filepath, Snackbar.LENGTH_LONG);
CommonUtil.makeSnackBar(mViewPager, getString(R.string.pic_saved) + filepath, Snackbar.LENGTH_LONG);
break;
default:
break;
Expand All @@ -67,6 +59,7 @@ public void handleMessage(Message msg) {
private ViewPager mViewPager;
private List<Image> mImages;
private int mPos;
private int mSavedPicPos = -1;
private Toolbar mToolbar;
private Realm mRealm;
private FragmentStatePagerAdapter mAdapter;
Expand All @@ -92,6 +85,12 @@ public void onChange() {

mImages = Image.all(mRealm);
mViewPager = (ViewPager) findViewById(R.id.viewer_pager);
mViewPager.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
mViewPager.setAdapter(mAdapter = new FragmentStatePagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int position) {
Expand All @@ -115,7 +114,6 @@ public int getCount() {
setEnterSharedElementCallback(new SharedElementCallback() {
@Override
public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) {
// Log.d(TAG, "onMapSharedElements: " + mViewPager.getCurrentItem());
Image image = mImages.get(mViewPager.getCurrentItem());
sharedElements.clear();
sharedElements.put(image.getUrl(), ((ViewerFragment) mAdapter.instantiateItem(mViewPager, mViewPager.getCurrentItem())).getSharedElement());
Expand Down Expand Up @@ -145,33 +143,22 @@ public boolean onOptionsItemSelected(MenuItem item) {
supportFinishAfterTransition();
return true;
case R.id.img_save:
mSavedPicPos = mViewPager.getCurrentItem();
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
//申请WRITE_EXTERNAL_STORAGE权限
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
WRITE_EXTERNAL_STORAGE_REQUEST_CODE);
} else {
final String url = mImages.get(mViewPager.getCurrentItem()).getUrl();
new Thread(new Runnable() {
@Override
public void run() {
try {
PicUtil.saveBitmapFromUrl(ViewerActivity.this, url, msgHandler);
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
savePicAt(mSavedPicPos);
}
return true;
case R.id.img_share:
final String url = mImages.get(mViewPager.getCurrentItem()).getUrl();
new Thread(new Runnable() {
@Override
public void run() {
shareMsg(getString(R.string.str_share_msg), null, url);
shareMsg(getString(R.string.share_msg), null, url);
}
}).start();
return true;
Expand All @@ -180,9 +167,27 @@ public void run() {
}
}

private void savePicAt(int pos) {
if (pos < 0)
return;

final String url = mImages.get(pos).getUrl();
new Thread(new Runnable() {
@Override
public void run() {
try {
PicUtil.saveBitmapFromUrl(ViewerActivity.this, url, msgHandler);
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}

@Override
public void supportFinishAfterTransition() {
// Log.d(TAG, "supportFinishAfterTransition: finish");
Intent data = new Intent();
data.putExtra("index", mViewPager.getCurrentItem());
setResult(RESULT_OK, data);
Expand All @@ -195,6 +200,7 @@ private void initToolbar() {
mToolbar.setTitle(R.string.nav_girls);
mToolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
mToolbar.setTitleTextColor(getResources().getColor(android.R.color.white));
mToolbar.setBackgroundColor(getResources().getColor(android.R.color.transparent));
setSupportActionBar(mToolbar);
}

Expand Down Expand Up @@ -236,7 +242,8 @@ public void toggleToolbar() {
}

public void hideToolbar() {
mToolbar.animate().translationY(-mToolbar.getHeight()).setInterpolator(new AccelerateInterpolator(2));
FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mToolbar.getLayoutParams();
mToolbar.animate().translationY(-(mToolbar.getHeight() + lp.topMargin)).setInterpolator(new AccelerateInterpolator(2));
mIsHidden = true;
}

Expand All @@ -248,10 +255,10 @@ public void showToolbar() {
}


// @Override
// public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
// super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// if (requestCode == WRITE_EXTERNAL_STORAGE_REQUEST_CODE)
// PicUtil.saveBitmapFromUrl(ScalableImageActivity.this, mUrls.get(mViewPager.getCurrentItem()));
// }
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == WRITE_EXTERNAL_STORAGE_REQUEST_CODE)
savePicAt(mSavedPicPos);
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.RequestFuture;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.Target;
import com.example.ivor_hu.meizhi.GirlsFragment;
import com.example.ivor_hu.meizhi.db.Image;
import com.example.ivor_hu.meizhi.net.ImageFetcher;
import com.example.ivor_hu.meizhi.utils.Constants;
Expand Down Expand Up @@ -98,7 +96,7 @@ private void sendResult(Intent intent, Realm realm, int fetched) {


private int fetchLatest(final Realm realm) throws InterruptedException, ExecutionException, ParseException, JSONException {
RequestFuture<JSONObject> future = VolleyUtil.getInstance(this).getJSONSync(Constants.LATEST_GIRLS_URL, GirlsFragment.VOLLEY_TAG);
RequestFuture<JSONObject> future = VolleyUtil.getInstance(this).getJSONSync(Constants.LATEST_GIRLS_URL, Constants.TYPE_GIRLS);
return updateImages(realm, future);
}

Expand All @@ -117,14 +115,13 @@ private int fetchMore(Realm realm, Date publishedAt) throws InterruptedException
private int fetch(Realm realm, String baseline, List<String> dates) throws InterruptedException, ExecutionException, JSONException, ParseException {
int fetched = 0;
for (String date : dates) {
Log.d(TAG, "fetchRefresh: " + date);
if (date == null)
return fetched;

if (date.equals(baseline))
continue;

RequestFuture<JSONObject> imgFuture = VolleyUtil.getInstance(this).getJSONSync(Constants.DAYLY_DATA_URL + date, GirlsFragment.VOLLEY_TAG);
RequestFuture<JSONObject> imgFuture = VolleyUtil.getInstance(this).getJSONSync(Constants.DAYLY_DATA_URL + date, Constants.TYPE_GIRLS);
JSONObject imgResponse = imgFuture.get();
if (imgResponse.getBoolean("error"))
continue;
Expand Down
Loading

0 comments on commit 347cde9

Please sign in to comment.