Java tutorial
package com.jgraves.achievementunlocked; /* * Copyright 2012 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.Color; import android.graphics.Typeface; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v4.app.Fragment; import android.util.Log; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.ViewTreeObserver; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import com.android.volley.Request; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.ImageLoader; import com.android.volley.toolbox.ImageRequest; import com.android.volley.toolbox.JsonObjectRequest; import com.androidquery.AQuery; import com.facebook.widget.ProfilePictureView; import com.jgraves.achievementunlocked.libraries.ObservableScrollView; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.lang.String; import java.util.ArrayList; import java.util.HashMap; class AchievementsList_Fragment extends Fragment implements ObservableScrollView.Callbacks { private LinearLayout mQuickReturnView; private ScrollSettleHandler mScrollSettleHandler = new ScrollSettleHandler(); private int mMinRawY = 0; private int mState = STATE_ONSCREEN; private int mQuickReturnHeight; private int mMaxScrollY; private ProfilePictureView profilePictureView; private AQuery aq; private View rootView; private Context context; private int imageViewHeight; private LinearLayout ll_images_container; private ObservableScrollView sv_images; private FrameLayout ll_main; private TextView tv_name; private TextView tv_score; private ArrayList<ImageView> imageViews = new ArrayList<ImageView>(); Typeface robotoTypeface; private HashMap<View, Long> touchMap = new HashMap<View, Long>(); @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { context = getActivity(); rootView = inflater.inflate(R.layout.achievements_list, null); ll_main = (FrameLayout) rootView.findViewById(R.id.ll_main); ll_images_container = (LinearLayout) rootView.findViewById(R.id.ll_images); sv_images = (ObservableScrollView) rootView.findViewById(R.id.sv_images); sv_images.setCallbacks(this); mQuickReturnView = (LinearLayout) rootView.findViewById(R.id.sticky); tv_name = (TextView) rootView.findViewById(R.id.tv_name); tv_name.setText("Jordan Graves"); tv_score = (TextView) rootView.findViewById(R.id.tv_score); tv_score.setText("Score: 150"); profilePictureView = (ProfilePictureView) rootView.findViewById(R.id.selection_profile_pic); profilePictureView.setCropped(true); profilePictureView.setProfileId(ExploraApp.FACEBOOK_ID); sv_images.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { onScrollChanged(sv_images.getScrollY()); mMaxScrollY = sv_images.computeVerticalScrollRange() - sv_images.getHeight(); mQuickReturnHeight = mQuickReturnView.getHeight(); } }); return rootView; } @Override public void onResume() { super.onResume(); ll_images_container.removeAllViews(); getAllUserAchievements(ExploraApp.USER_ID); int activity_height = ExploraApp.screenHeight - ExploraApp.statusBarHeight - (ExploraApp.actionBarHeight * 2); imageViewHeight = (int) (activity_height * 0.25); } @Override public void onDestroyView() { super.onDestroyView(); } public void getAllUserAchievements(String user_id) { JSONObject body = new JSONObject(); Log.i(ExploraApp.TAG, "Getting all User Achievments IDs. user_id " + user_id); try { body.put("user_id", user_id); String url = "https://achievement-unlocked-03.appspot.com/user/achievements/all"; JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, body, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject jsonObject) { try { Log.i(ExploraApp.TAG, "All Achievements Response: " + jsonObject.toString()); Log.i(ExploraApp.TAG, "'achievements' : " + jsonObject.getJSONArray("achievements").toString()); JSONArray achievements = jsonObject.getJSONArray("achievements"); for (int i = 0; i < achievements.length(); i++) { JSONObject achievementInfo = achievements.getJSONObject(i); Log.i(ExploraApp.TAG, "All Achievements, Achievement at: " + i + " : " + achievementInfo.toString()); addAchievementToList(achievementInfo.getLong("id"), achievementInfo.getString("name"), achievementInfo.getInt("points")); } } catch (JSONException e) { Log.i(ExploraApp.TAG, "JSON Exception: " + e.getMessage()); e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { } }); ExploraApp.mRequestQueue.add(request); } catch (JSONException e) { e.printStackTrace(); } } private void addAchievementToList(Long id, String name, int points) { Log.i(ExploraApp.TAG, "Adding achievement image request of id " + id + " with height, " + imageViewHeight); FrameLayout fl = new FrameLayout(getActivity()); touchMap.put(fl, id); fl.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Long id = touchMap.get(v); Log.i(ExploraApp.TAG, "TOUCHED " + id); Intent intent = new Intent(getActivity(), Activity_AchievementInfo.class); intent.putExtra("achievement_id", Long.toString(id)); startActivity(intent); } }); TextView tv_name = new TextView(getActivity()); TextView tv_points = new TextView(getActivity()); ImageView iv = new ImageView(getActivity()); fl.setMinimumHeight(imageViewHeight); fl.setMinimumWidth(ExploraApp.screenWidth); robotoTypeface = Typeface.createFromAsset(getActivity().getAssets(), "Roboto-Thin.ttf"); tv_name.setText(name); tv_name.setTextSize(15f); tv_points.setTextSize(15f); tv_name.setTypeface(robotoTypeface); tv_points.setTypeface(robotoTypeface); tv_points.setText(Integer.toString(points)); tv_points.setGravity(Gravity.RIGHT); tv_points.setTextColor(Color.WHITE); tv_name.setTextColor(Color.WHITE); iv.setMinimumHeight(imageViewHeight); iv.setMinimumWidth(ExploraApp.screenWidth); fl.addView(iv); fl.addView(tv_name); fl.addView(tv_points); ll_images_container.addView(fl); sv_images.bringChildToFront(mQuickReturnView); DefaultImageListener listener = new DefaultImageListener(iv); ImageRequest imageRequest = new ImageRequest( ExploraApp.url_main + "/achievement/" + id + "/photo?y=" + imageViewHeight + "&x=" + ExploraApp.screenWidth, listener, ExploraApp.screenWidth, imageViewHeight, Bitmap.Config.ARGB_8888, listener); ExploraApp.mRequestQueue.add(imageRequest); } private static final int STATE_ONSCREEN = 0; private static final int STATE_OFFSCREEN = 1; private static final int STATE_RETURNING = 2; @Override public void onScrollChanged(int scrollY) { scrollY = Math.min(mMaxScrollY, scrollY); mScrollSettleHandler.onScroll(scrollY); int rawY = -scrollY; int translationY = 0; switch (mState) { case STATE_OFFSCREEN: if (rawY <= mMinRawY) { mMinRawY = rawY; } else { mState = STATE_RETURNING; } translationY = rawY; break; case STATE_ONSCREEN: if (rawY < -mQuickReturnHeight) { mState = STATE_OFFSCREEN; mMinRawY = rawY; } translationY = rawY; break; case STATE_RETURNING: translationY = (rawY - mMinRawY) - mQuickReturnHeight; if (translationY > 0) { translationY = 0; mMinRawY = rawY - mQuickReturnHeight; } if (rawY > 0) { mState = STATE_ONSCREEN; translationY = rawY; } if (translationY < -mQuickReturnHeight) { mState = STATE_OFFSCREEN; mMinRawY = rawY; } break; } mQuickReturnView.animate().cancel(); mQuickReturnView.setTranslationY(translationY + scrollY); } @Override public void onDownMotionEvent() { mScrollSettleHandler.setSettleEnabled(false); } @Override public void onUpOrCancelMotionEvent() { mScrollSettleHandler.setSettleEnabled(true); mScrollSettleHandler.onScroll(sv_images.getScrollY()); } private class ScrollSettleHandler extends Handler { private static final int SETTLE_DELAY_MILLIS = 100; private int mSettledScrollY = Integer.MIN_VALUE; private boolean mSettleEnabled; public void onScroll(int scrollY) { if (mSettledScrollY != scrollY) { // Clear any pending messages and post delayed removeMessages(0); sendEmptyMessageDelayed(0, SETTLE_DELAY_MILLIS); mSettledScrollY = scrollY; } } public void setSettleEnabled(boolean settleEnabled) { mSettleEnabled = settleEnabled; } @Override public void handleMessage(Message msg) { // Handle the scroll settling. if (STATE_RETURNING == mState && mSettleEnabled) { int mDestTranslationY; if (mSettledScrollY - mQuickReturnView.getTranslationY() > mQuickReturnHeight / 2) { mState = STATE_OFFSCREEN; mDestTranslationY = Math.max(mSettledScrollY - mQuickReturnHeight, 0); } else { mDestTranslationY = mSettledScrollY; } mMinRawY = -mQuickReturnHeight - mDestTranslationY; mQuickReturnView.animate().translationY(mDestTranslationY); } mSettledScrollY = Integer.MIN_VALUE; // reset } } private class DefaultImageListener implements ImageLoader.ImageListener, Response.Listener<Bitmap> { private ImageView imageView; public DefaultImageListener(ImageView view) { imageView = view; } @Override public void onErrorResponse(VolleyError error) { } @Override public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) { Log.d(ExploraApp.TAG, " ImageListener, onResponse1"); setDimensions(imageView, 0, imageViewHeight); if (response.getBitmap() != null) { imageView.setImageBitmap(response.getBitmap()); sv_images.bringChildToFront(mQuickReturnView); } } @Override public void onResponse(Bitmap bitmap) { Log.d(ExploraApp.TAG, " ImageListener, onResponse2"); setDimensions(imageView, 0, imageViewHeight); imageView.setAlpha(0.5f); imageView.setImageBitmap(bitmap); ll_main.bringChildToFront(mQuickReturnView); } void setDimensions(View v, int top, int bottom) { v.setTop(top); v.setLeft(0); v.setBottom(bottom); v.setRight(ExploraApp.screenWidth); } } }