Java tutorial
/** * Copyright Google Inc. All Rights Reserved. * <p/> * 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 * <p/> * http://www.apache.org/licenses/LICENSE-2.0 * <p/> * 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.firebase.codelab.friendlychat; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.content.ContextCompat; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.text.Editable; import android.text.InputFilter; import android.text.TextWatcher; 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.widget.Button; import android.widget.EditText; import android.widget.ProgressBar; import android.widget.TextView; import com.bumptech.glide.Glide; import com.firebase.ui.database.FirebaseRecyclerAdapter; import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.AdView; import com.google.android.gms.appinvite.AppInviteInvitation; import com.google.android.gms.auth.api.Auth; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.api.GoogleApiClient; import com.google.android.gms.tasks.OnFailureListener; import com.google.android.gms.tasks.OnSuccessListener; import com.google.firebase.analytics.FirebaseAnalytics; import com.google.firebase.appindexing.Action; import com.google.firebase.appindexing.FirebaseAppIndex; import com.google.firebase.appindexing.FirebaseUserActions; import com.google.firebase.appindexing.Indexable; import com.google.firebase.appindexing.builders.Indexables; import com.google.firebase.appindexing.builders.PersonBuilder; import com.google.firebase.auth.FirebaseAuth; import com.google.firebase.auth.FirebaseUser; import com.google.firebase.crash.FirebaseCrash; import com.google.firebase.database.DataSnapshot; import com.google.firebase.database.DatabaseReference; import com.google.firebase.database.FirebaseDatabase; import com.google.firebase.remoteconfig.FirebaseRemoteConfig; import com.google.firebase.remoteconfig.FirebaseRemoteConfigSettings; import java.util.HashMap; import java.util.Map; import de.hdodenhof.circleimageview.CircleImageView; import static android.app.Activity.RESULT_OK; public class ChatFragment extends Fragment implements GoogleApiClient.OnConnectionFailedListener { public static class MessageViewHolder extends RecyclerView.ViewHolder { public TextView messageTextView; public TextView messengerTextView; public CircleImageView messengerImageView; public MessageViewHolder(View v) { super(v); messageTextView = (TextView) itemView.findViewById(R.id.messageTextView); messengerTextView = (TextView) itemView.findViewById(R.id.messengerTextView); messengerImageView = (CircleImageView) itemView.findViewById(R.id.messengerImageView); } } private static final String TAG = "ChatFragment"; public static final String MESSAGES_CHILD = "messages"; private static final int REQUEST_INVITE = 1; public static final int DEFAULT_MSG_LENGTH_LIMIT = 10; public static final String ANONYMOUS = "anonymous"; private static final String MESSAGE_SENT_EVENT = "message_sent"; private static final String MESSAGE_URL = "http://friendlychat.firebase.google.com/message/"; private String mUsername; private String mPhotoUrl; private SharedPreferences mSharedPreferences; private Button mSendButton; private RecyclerView mMessageRecyclerView; private LinearLayoutManager mLinearLayoutManager; private FirebaseRecyclerAdapter<FriendlyMessage, MessageViewHolder> mFirebaseAdapter; private ProgressBar mProgressBar; private DatabaseReference mFirebaseDatabaseReference; private FirebaseAuth mFirebaseAuth; private FirebaseUser mFirebaseUser; private FirebaseAnalytics mFirebaseAnalytics; private EditText mMessageEditText; private AdView mAdView; private FirebaseRemoteConfig mFirebaseRemoteConfig; private GoogleApiClient mGoogleApiClient; private EditText messageEditText; private Button button; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.chat_fragment, container, false); messageEditText = (EditText) view.findViewById(R.id.messageEditText); button = (Button) view.findViewById(R.id.sendButton); return view; } public void onStart() { super.onStart(); mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this.getActivity()); mUsername = ANONYMOUS; // Initialize Firebase Auth mFirebaseAuth = FirebaseAuth.getInstance(); mFirebaseUser = mFirebaseAuth.getCurrentUser(); mUsername = mFirebaseUser.getDisplayName(); mPhotoUrl = mFirebaseUser.getPhotoUrl().toString(); mProgressBar = (ProgressBar) getView().findViewById(R.id.progressBar); mMessageRecyclerView = (RecyclerView) getView().findViewById(R.id.messageRecyclerView); mLinearLayoutManager = new LinearLayoutManager(getActivity()); mLinearLayoutManager.setStackFromEnd(true); mFirebaseDatabaseReference = FirebaseDatabase.getInstance().getReference(); mFirebaseAdapter = new FirebaseRecyclerAdapter<FriendlyMessage, MessageViewHolder>(FriendlyMessage.class, R.layout.item_message, MessageViewHolder.class, mFirebaseDatabaseReference.child(MESSAGES_CHILD)) { @Override protected FriendlyMessage parseSnapshot(DataSnapshot snapshot) { FriendlyMessage friendlyMessage = super.parseSnapshot(snapshot); if (friendlyMessage != null) { friendlyMessage.setId(snapshot.getKey()); } return friendlyMessage; } @Override protected void populateViewHolder(MessageViewHolder viewHolder, FriendlyMessage friendlyMessage, int position) { mProgressBar.setVisibility(ProgressBar.INVISIBLE); viewHolder.messageTextView.setText(friendlyMessage.getText()); viewHolder.messengerTextView.setText(friendlyMessage.getName()); if (friendlyMessage.getPhotoUrl() == null) { viewHolder.messengerImageView.setImageDrawable( ContextCompat.getDrawable(getActivity(), R.drawable.ic_account_circle_black_36dp)); } else { Glide.with(ChatFragment.this).load(friendlyMessage.getPhotoUrl()) .into(viewHolder.messengerImageView); } // write this message to the on-device index FirebaseAppIndex.getInstance().update(getMessageIndexable(friendlyMessage)); // log a view action on it FirebaseUserActions.getInstance().end(getMessageViewAction(friendlyMessage)); } }; mFirebaseAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() { @Override public void onItemRangeInserted(int positionStart, int itemCount) { super.onItemRangeInserted(positionStart, itemCount); int friendlyMessageCount = mFirebaseAdapter.getItemCount(); int lastVisiblePosition = mLinearLayoutManager.findLastCompletelyVisibleItemPosition(); // If the recycler view is initially being loaded or the user is at the bottom of the list, scroll // to the bottom of the list to show the newly added message. if (lastVisiblePosition == -1 || (positionStart >= (friendlyMessageCount - 1) && lastVisiblePosition == (positionStart - 1))) { mMessageRecyclerView.scrollToPosition(positionStart); } } }); mMessageRecyclerView.setLayoutManager(mLinearLayoutManager); mMessageRecyclerView.setAdapter(mFirebaseAdapter); // Initialize Firebase Measurement. mFirebaseAnalytics = FirebaseAnalytics.getInstance(getActivity()); // Initialize Firebase Remote Config. mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance(); // Define Firebase Remote Config Settings. FirebaseRemoteConfigSettings firebaseRemoteConfigSettings = new FirebaseRemoteConfigSettings.Builder() .setDeveloperModeEnabled(true).build(); // Define default config values. Defaults are used when fetched config values are not // available. Eg: if an error occurred fetching values from the server. Map<String, Object> defaultConfigMap = new HashMap<>(); defaultConfigMap.put("friendly_msg_length", 1000L); // Apply config settings and default values. mFirebaseRemoteConfig.setConfigSettings(firebaseRemoteConfigSettings); mFirebaseRemoteConfig.setDefaults(defaultConfigMap); // Fetch remote config. fetchConfig(); mMessageEditText = (EditText) getView().findViewById(R.id.messageEditText); mMessageEditText.setFilters(new InputFilter[] { new InputFilter.LengthFilter( mSharedPreferences.getInt(CodelabPreferences.FRIENDLY_MSG_LENGTH, DEFAULT_MSG_LENGTH_LIMIT)) }); mMessageEditText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { if (charSequence.toString().trim().length() > 0) { mSendButton.setEnabled(true); } else { mSendButton.setEnabled(false); } } @Override public void afterTextChanged(Editable editable) { } }); mSendButton = (Button) getView().findViewById(R.id.sendButton); mSendButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { FriendlyMessage friendlyMessage = new FriendlyMessage(mMessageEditText.getText().toString(), mUsername, mPhotoUrl); mFirebaseDatabaseReference.child(MESSAGES_CHILD).push().setValue(friendlyMessage); mMessageEditText.setText(""); mFirebaseAnalytics.logEvent(MESSAGE_SENT_EVENT, null); } }); } private Action getMessageViewAction(FriendlyMessage friendlyMessage) { return new Action.Builder(Action.Builder.VIEW_ACTION) .setObject(friendlyMessage.getName(), MESSAGE_URL.concat(friendlyMessage.getId())) .setMetadata(new Action.Metadata.Builder().setUpload(false)).build(); } private Indexable getMessageIndexable(FriendlyMessage friendlyMessage) { PersonBuilder sender = Indexables.personBuilder().setIsSelf(mUsername == friendlyMessage.getName()) .setName(friendlyMessage.getName()).setUrl(MESSAGE_URL.concat(friendlyMessage.getId() + "/sender")); PersonBuilder recipient = Indexables.personBuilder().setName(mUsername) .setUrl(MESSAGE_URL.concat(friendlyMessage.getId() + "/recipient")); Indexable messageToIndex = Indexables.messageBuilder().setName(friendlyMessage.getText()) .setUrl(MESSAGE_URL.concat(friendlyMessage.getId())).setSender(sender).setRecipient(recipient) .build(); return messageToIndex; } @Override public void onPause() { if (mAdView != null) { mAdView.pause(); } super.onPause(); } @Override public void onResume() { super.onResume(); if (mAdView != null) { mAdView.resume(); } } @Override public void onDestroy() { if (mAdView != null) { mAdView.destroy(); } super.onDestroy(); } private void causeCrash() { throw new NullPointerException("Fake null pointer exception"); } private void sendInvitation() { Intent intent = new AppInviteInvitation.IntentBuilder(getString(R.string.invitation_title)) .setMessage(getString(R.string.invitation_message)) .setCallToActionText(getString(R.string.invitation_cta)).build(); startActivityForResult(intent, REQUEST_INVITE); } // Fetch the config to determine the allowed length of messages. public void fetchConfig() { long cacheExpiration = 3600; // 1 hour in seconds // If developer mode is enabled reduce cacheExpiration to 0 so that each fetch goes to the // server. This should not be used in release builds. if (mFirebaseRemoteConfig.getInfo().getConfigSettings().isDeveloperModeEnabled()) { cacheExpiration = 0; } mFirebaseRemoteConfig.fetch(cacheExpiration).addOnSuccessListener(new OnSuccessListener<Void>() { @Override public void onSuccess(Void aVoid) { // Make the fetched config available via FirebaseRemoteConfig get<type> calls. mFirebaseRemoteConfig.activateFetched(); applyRetrievedLengthLimit(); } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { // There has been an error fetching the config Log.w(TAG, "Error fetching config: " + e.getMessage()); applyRetrievedLengthLimit(); } }); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); Log.d(TAG, "onActivityResult: requestCode=" + requestCode + ", resultCode=" + resultCode); if (requestCode == REQUEST_INVITE) { if (resultCode == RESULT_OK) { // Use Firebase Measurement to log that invitation was sent. Bundle payload = new Bundle(); payload.putString(FirebaseAnalytics.Param.VALUE, "inv_sent"); // Check how many invitations were sent and log. String[] ids = AppInviteInvitation.getInvitationIds(resultCode, data); Log.d(TAG, "Invitations sent: " + ids.length); } else { // Use Firebase Measurement to log that invitation was not sent Bundle payload = new Bundle(); payload.putString(FirebaseAnalytics.Param.VALUE, "inv_not_sent"); mFirebaseAnalytics.logEvent(FirebaseAnalytics.Event.SHARE, payload); // Sending failed or it was canceled, show failure message to the user Log.d(TAG, "Failed to send invitation."); } } } /** * Apply retrieved length limit to edit text field. This result may be fresh from the server or it may be from * cached values. */ private void applyRetrievedLengthLimit() { Long friendly_msg_length = mFirebaseRemoteConfig.getLong("friendly_msg_length"); mMessageEditText .setFilters(new InputFilter[] { new InputFilter.LengthFilter(friendly_msg_length.intValue()) }); Log.d(TAG, "FML is: " + friendly_msg_length); } @Override public void onConnectionFailed(ConnectionResult connectionResult) { Log.d(TAG, "onConnectionFailed:" + connectionResult); } }