Java tutorial
/* * Copyright (c) 2014 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. */ package com.google.android.apps.watchme; import android.app.Activity; import android.app.AlertDialog; import android.app.Fragment; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.ClipData; import android.content.DialogInterface; import android.content.IntentSender; import android.media.Image; import android.os.Bundle; import android.os.Message; import android.support.v4.widget.SwipeRefreshLayout; import android.text.ClipboardManager; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.GridView; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import com.google.android.apps.watchme.util.EventData; import com.google.android.apps.watchme.util.ImageFetcher; import com.google.android.apps.watchme.util.ImageWorker; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks; import com.google.android.gms.common.api.GoogleApiClient; import com.google.android.gms.plus.Plus; import com.google.android.gms.plus.PlusOneButton; import com.google.android.gms.plus.model.people.Person; import java.util.List; /** * @author Ibrahim Ulukaya <ulukaya@google.com> * <p/> * Left side fragment showing user's uploaded YouTube videos. */ public class EventsListFragment extends Fragment implements ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks { private static final String TAG = EventsListFragment.class.getName(); private Callbacks mCallbacks; private ImageWorker mImageFetcher; private GoogleApiClient mGoogleApiClient; private GridView mGridView; public ImageView thumbnailpicture; public EventsListFragment() { } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mGoogleApiClient = new GoogleApiClient.Builder(getActivity()).addApi(Plus.API) .addScope(Plus.SCOPE_PLUS_PROFILE).addConnectionCallbacks(this).addOnConnectionFailedListener(this) .build(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View listView = inflater.inflate(R.layout.list_fragment, container, false); mGridView = (GridView) listView.findViewById(R.id.grid_view); TextView emptyView = (TextView) listView.findViewById(android.R.id.empty); mGridView.setEmptyView(emptyView); emptyView.setVisibility(View.GONE); ImageView downimage = (ImageView) listView.findViewById(R.id.down); mGridView.setEmptyView(downimage); return listView; } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); setProfileInfo(); } public void setEvents(List<EventData> events) { if (!isAdded()) { return; } mGridView.setAdapter(new LiveEventAdapter(events)); } public void setProfileInfo() { //FOR TOP AREA OF SCREEN /* if (!mGoogleApiClient.isConnected() || Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) == null) { ((ImageView) getView().findViewById(R.id.avatar)) .setImageDrawable(null); ((TextView) getView().findViewById(R.id.display_name)) .setText(R.string.not_signed_in); } else { Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient); if (currentPerson.hasImage()) { mImageFetcher.loadImage(currentPerson.getImage().getUrl(), ((ImageView) getView().findViewById(R.id.avatar))); } if (currentPerson.hasDisplayName()) { ((TextView) getView().findViewById(R.id.display_name)) .setText(currentPerson.getDisplayName()); } } */ } @Override public void onResume() { super.onResume(); mGoogleApiClient.connect(); } @Override public void onPause() { super.onPause(); mGoogleApiClient.disconnect(); } @Override public void onConnected(Bundle bundle) { if (mGridView.getAdapter() != null) { ((LiveEventAdapter) mGridView.getAdapter()).notifyDataSetChanged(); } setProfileInfo(); mCallbacks.onConnected(Plus.AccountApi.getAccountName(mGoogleApiClient)); } @Override public void onConnectionSuspended(int i) { } @Override public void onDisconnected() { } @Override public void onConnectionFailed(ConnectionResult connectionResult) { if (connectionResult.hasResolution()) { Toast.makeText(getActivity(), R.string.connection_to_google_play_failed, Toast.LENGTH_SHORT).show(); Log.e(TAG, String.format("Connection to Play Services Failed, error: %d, reason: %s", connectionResult.getErrorCode(), connectionResult.toString())); try { connectionResult.startResolutionForResult(getActivity(), 0); } catch (IntentSender.SendIntentException e) { Log.e(TAG, e.toString(), e); } } } @Override public void onAttach(Activity activity) { super.onAttach(activity); if (!(activity instanceof Callbacks)) { throw new ClassCastException("Activity must implement callbacks."); } mCallbacks = (Callbacks) activity; mImageFetcher = mCallbacks.onGetImageFetcher(); } @Override public void onDetach() { super.onDetach(); mCallbacks = null; mImageFetcher = null; } public interface Callbacks { public ImageFetcher onGetImageFetcher(); public void onEventSelected(EventData event); public void onConnected(String connectedAccountName); } private class LiveEventAdapter extends BaseAdapter { private List<EventData> mEvents; private LiveEventAdapter(List<EventData> events) { mEvents = events; } @Override public int getCount() { return mEvents.size(); } @Override public Object getItem(int i) { return mEvents.get(i); } @Override public long getItemId(int i) { return mEvents.get(i).getId().hashCode(); } @Override public View getView(final int position, View convertView, ViewGroup container) { if (convertView == null) { convertView = LayoutInflater.from(getActivity()).inflate(R.layout.list_item, container, false); } EventData event = mEvents.get(position); ((TextView) convertView.findViewById(android.R.id.text1)).setText(event.getTitle()); //ignoring default thumbnail cuz it looks bad //mImageFetcher.loadImage(event.getThumbUri(), // (ImageView) convertView.findViewById(R.id.thumbnail)); if (mGoogleApiClient.isConnected()) { ((PlusOneButton) convertView.findViewById(R.id.plus_button)).initialize(event.getWatchUri(), null); //displays URL of video: event.getWatchUri() } final String urltext = event.getWatchUri(); //create textview to make text copiable final TextView showText = new TextView(getActivity()); showText.setText(urltext); showText.setTextIsSelectable(true); ((Button) convertView.findViewById(R.id.url)).setOnClickListener(new View.OnClickListener() { public void onClick(View v) { //used to get around final error TextView showText2 = showText; AlertDialog ad = new AlertDialog.Builder(getActivity()).create(); ad.setCancelable(false); ad.setTitle("URL of Stream"); if (showText2.getParent() == null) { ad.setView(showText2); } else { showText2 = null; //set it to null // now initialized yourView and its component again ad.setMessage(urltext); } ad.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { //... } }); ad.show(); } }); convertView.findViewById(R.id.main_target).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mCallbacks.onEventSelected(mEvents.get(position)); } }); return convertView; } } }