Java tutorial
/* * Copyright (C) 2014 The Android Open Source Project * * 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. */ package com.uncc.finalproject; import android.content.DialogInterface; import android.graphics.Color; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.v4.app.Fragment; import android.support.v7.app.AlertDialog; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.inputmethod.InputMethodManager; import android.widget.Toast; import com.parse.FindCallback; import com.parse.GetCallback; import com.parse.ParseACL; import com.parse.ParseException; import com.parse.ParseObject; import com.parse.ParseQuery; import com.parse.ParseUser; import java.util.ArrayList; import java.util.List; /** * Demonstrates the use of {@link RecyclerView} with a {@link LinearLayoutManager} and a * {@link GridLayoutManager}. */ public class AlbumGalleryFragment extends Fragment { private static final String TAG = "GalleryFragment"; private static final String KEY_LAYOUT_MANAGER = "layoutManager"; private static final int SPAN_COUNT = 2; private static final int DATASET_COUNT = 60; private List<String> user_names = new ArrayList<>(); private List<String> user_ids = new ArrayList<>(); private List<Boolean> user_names_selected = new ArrayList<>(); private boolean[] primitive_boolean_selected = null; private List<String> user_names_sendoff_list = new ArrayList<>(); private CharSequence[] charSequenceItems = null; private String album_name = null; private String album_id = null; private ParseACL albumACL; private ParseObject album_object = null; int checked_counter; private enum LayoutManagerType { LINEAR_LAYOUT_MANAGER } protected LayoutManagerType mCurrentLayoutManagerType; protected RecyclerView mRecyclerView; protected CustomAdapter mAdapter; protected RecyclerView.LayoutManager mLayoutManager; protected ArrayList<ParseObject> mDataset = new ArrayList<>(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public void onResume() { super.onResume(); View view = getActivity().getCurrentFocus(); if (view != null) { InputMethodManager imm = (InputMethodManager) getActivity() .getSystemService(getContext().INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(view.getWindowToken(), 0); } Bundle bundle = this.getArguments(); album_name = bundle.getString("album_name"); album_id = bundle.getString("album_id"); callTitleBarSet(album_name); ParseObject temp = new ParseObject("Albums"); temp.add("photo_name", "Album is Empty"); mDataset.add(temp); ParseQuery<ParseObject> album_query = ParseQuery.getQuery("Photo"); album_query.whereEqualTo("album_name", album_name); album_query.orderByDescending("createdAt"); album_query.findInBackground(new FindCallback<ParseObject>() { @Override public void done(List<ParseObject> objects, ParseException e) { if (e == null) { mDataset = new ArrayList<>(objects); mAdapter.updateArrayList(mDataset); } else { Log.d("demo", "recycler failed"); } } }); ParseQuery<ParseObject> album = ParseQuery.getQuery("Albums"); album.whereEqualTo("album_name", album_name); album.getFirstInBackground(new GetCallback<ParseObject>() { @Override public void done(ParseObject album, ParseException e) { if (e == null) { album_object = album; ParseUser currentUser = ParseUser.getCurrentUser(); ParseQuery<ParseUser> query = ParseUser.getQuery(); query.whereNotEqualTo("objectId", currentUser.getObjectId()); query.findInBackground(new FindCallback<ParseUser>() { public void done(List<ParseUser> objects, ParseException e) { if (e == null) { primitive_boolean_selected = new boolean[objects.size()]; int counter = 0; for (ParseObject obj : objects) { user_names.add(obj.get("username").toString()); user_ids.add(obj.getObjectId()); if (album_object.getACL().getPublicReadAccess()) { primitive_boolean_selected[counter] = true; } else { primitive_boolean_selected[counter] = album_object.getACL() .getReadAccess(obj.getObjectId()); } user_names_selected.add(false); counter++; } charSequenceItems = user_names.toArray(new CharSequence[user_names.size()]); } else { Log.e("demo", e.toString()); } } }); } else { } } }); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_gallery, container, false); rootView.setTag(TAG); FloatingActionButton fab = (FloatingActionButton) rootView.findViewById(R.id.fab); fab.setImageResource(R.drawable.ic_menu_share); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle("Select Users"); builder.setMultiChoiceItems(charSequenceItems, primitive_boolean_selected, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { primitive_boolean_selected[which] = isChecked; } }).setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { albumACL = new ParseACL(); checked_counter = 0; for (int counter = 0; counter < primitive_boolean_selected.length; counter++) { albumACL.setReadAccess(user_ids.get(counter), primitive_boolean_selected[counter]); albumACL.setWriteAccess(user_ids.get(counter), primitive_boolean_selected[counter]); if (primitive_boolean_selected[counter]) checked_counter++; } if (user_names.size() > checked_counter) { Log.d("demo", "public " + user_names.size() + " > " + checked_counter); albumACL.setPublicReadAccess(false); albumACL.setPublicWriteAccess(false); } else { albumACL.setPublicReadAccess(true); albumACL.setPublicWriteAccess(true); } albumACL.setReadAccess(ParseUser.getCurrentUser(), true); albumACL.setWriteAccess(ParseUser.getCurrentUser(), true); ParseQuery<ParseObject> album = ParseQuery.getQuery("Albums"); album.whereEqualTo("album_name", album_name); album.getFirstInBackground(new GetCallback<ParseObject>() { @Override public void done(ParseObject object, ParseException e) { if (e == null) { object.setACL(albumACL); object.put("counterACL", checked_counter); object.saveInBackground(); Toast.makeText(getActivity(), "Album Updated", Toast.LENGTH_SHORT) .show(); } } }); } }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); } }).create().show(); } }); mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView); mLayoutManager = new LinearLayoutManager(getActivity()); mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER; if (savedInstanceState != null) { mCurrentLayoutManagerType = (LayoutManagerType) savedInstanceState.getSerializable(KEY_LAYOUT_MANAGER); } setRecyclerViewLayoutManager(mCurrentLayoutManagerType); mAdapter = new CustomAdapter(mDataset, getActivity(), getContext(), "photos"); mRecyclerView.setAdapter(mAdapter); setRecyclerViewLayoutManager(LayoutManagerType.LINEAR_LAYOUT_MANAGER); return rootView; } public void setRecyclerViewLayoutManager(LayoutManagerType layoutManagerType) { int scrollPosition = 0; if (mRecyclerView.getLayoutManager() != null) { scrollPosition = ((LinearLayoutManager) mRecyclerView.getLayoutManager()) .findFirstCompletelyVisibleItemPosition(); } mLayoutManager = new LinearLayoutManager(getActivity()); mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER; mRecyclerView.setLayoutManager(mLayoutManager); mRecyclerView.scrollToPosition(scrollPosition); } @Override public void onSaveInstanceState(Bundle savedInstanceState) { savedInstanceState.putSerializable(KEY_LAYOUT_MANAGER, mCurrentLayoutManagerType); super.onSaveInstanceState(savedInstanceState); } private void initDataset() { for (int i = 0; i < DATASET_COUNT; i++) { ParseObject temp = new ParseObject("Albums"); temp.add("album_name", "This is element #" + i); mDataset.add(temp); } } private void callTitleBarSet(String title) { if (getContext() == null) return; if (getContext() instanceof MainActivity) { MainActivity mainActivity = (MainActivity) getContext(); mainActivity.updateActionBarTitle(title); } } }