<commit_before>/* Copyright (c) 2008 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 sample;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import org.opensocial.android.OpenSocialActivity;
import org.opensocial.client.OpenSocialBatch;
import org.opensocial.client.OpenSocialClient;
import org.opensocial.client.OpenSocialProvider;
import org.opensocial.client.OpenSocialRequest;
import org.opensocial.client.Token;
import org.opensocial.data.OpenSocialPerson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Demo class. Prompts the user to choose an OpenSocial provider and then displays all of
 * their friends.
 *
 * @author <NAME>
 */
public class DisplayFriendsActivity extends Activity {
  private static String ANDROID_SCHEME = "x-opensocial-demo-app";
  private static Map<OpenSocialProvider, Token> SUPPORTED_PROVIDERS
      = new HashMap<OpenSocialProvider, Token>();
  public OpenSocialActivity util;

  static {
    // Setup all the OpenSocial containers you want to integrate with
    // and set your specific consumer token, if the container requires one.
    // example: SUPPORTED_PROVIDERS.put(OpenSocialProvider.MYSPACE,
    //     new Token(<your consumer key like "http://www.myspace.com/..." >, <your secret>));
    SUPPORTED_PROVIDERS.put(OpenSocialProvider.PLAXO, new Token("anonymous", ""));
  }

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setupClient();
  }

  private void setupClient() {
    util = new OpenSocialActivity(this, SUPPORTED_PROVIDERS, ANDROID_SCHEME);
    OpenSocialClient client = util.getOpenSocialClient();

    // If the client is null the OpenSocialChooserActivity will be started
    if (client != null) {
      showContacts(client, util.getProvider());
    }
  }

  private void showContacts(OpenSocialClient c, OpenSocialProvider provider) {
    List<OpenSocialPerson> friends = new ArrayList<OpenSocialPerson>();
    try {
      if (provider.isOpenSocial) {
        friends = c.fetchFriends();
      } else {
        OpenSocialBatch batch = new OpenSocialBatch();
        batch.addRequest(new OpenSocialRequest("@me/@all", ""), "friends");
        friends = batch.send(c).getItemAsPersonCollection("friends");
      }
    } catch (Exception e) {
      // TODO: Reduce the number of exceptions the library throws
      Log.i("DisplayFriendActivity", "Couldn't fetch friends from the container: "
          + e.getMessage());
    }

    LinearLayout linearLayout = new LinearLayout(this);
    linearLayout.setLayoutParams(new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.FILL_PARENT,
        LinearLayout.LayoutParams.FILL_PARENT));
    linearLayout.setOrientation(LinearLayout.VERTICAL);


    if (friends.size() > 0) {
      FriendListView contactsView = new FriendListView(this);
      contactsView.setFriends(friends);
      contactsView.setVerticalScrollBarEnabled(true);
      contactsView.setLayoutParams(new ViewGroup.LayoutParams(
          ViewGroup.LayoutParams.FILL_PARENT, 300));
      linearLayout.addView(contactsView);
    } else {
      TextView textView = new TextView(this);
      textView.setText("No contacts found.");
      linearLayout.addView(textView);
    }

    final DisplayFriendsActivity activity = this;

    Button clearAuthButton = new Button(this);
    clearAuthButton.setText("Clear Auth");
    clearAuthButton.setOnClickListener(new View.OnClickListener() {
      public void onClick(View view) {
        util.clearSavedAuthentication();
      }
    });

    Button fetchFriendsButton = new Button(this);
    fetchFriendsButton.setText("Fetch Friends");
    fetchFriendsButton.setOnClickListener(new View.OnClickListener() {
      public void onClick(View view) {<commit_msg>The sample android app no longer crashes when the user fetches friends twice in a row without clearing auth. <commit_after>/* Copyright (c) 2008 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 sample;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import org.opensocial.android.OpenSocialActivity;
import org.opensocial.client.OpenSocialBatch;
import org.opensocial.client.OpenSocialClient;
import org.opensocial.client.OpenSocialProvider;
import org.opensocial.client.OpenSocialRequest;
import org.opensocial.client.Token;
import org.opensocial.data.OpenSocialPerson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Demo class. Prompts the user to choose an OpenSocial provider and then displays all of
 * their friends.
 *
 * @author <NAME>
 */
public class DisplayFriendsActivity extends Activity {
  private static String ANDROID_SCHEME = "x-opensocial-demo-app";
  private static Map<OpenSocialProvider, Token> SUPPORTED_PROVIDERS
      = new HashMap<OpenSocialProvider, Token>();
  public OpenSocialActivity util;
  public OpenSocialClient client;

  static {
    // Setup all the OpenSocial containers you want to integrate with
    // and set your specific consumer token, if the container requires one.
    // example: SUPPORTED_PROVIDERS.put(OpenSocialProvider.MYSPACE,
    //     new Token(<your consumer key like "http://www.myspace.com/..." >, <your secret>));
    SUPPORTED_PROVIDERS.put(OpenSocialProvider.PLAXO, new Token("anonymous", ""));
  }

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    util = new OpenSocialActivity(this, SUPPORTED_PROVIDERS, ANDROID_SCHEME);
    setupClient();
  }

  private void setupClient() {
    // If the user fetches friends twice without clearing auth then we don't need to
    // recreate our oauth client
    if (client == null) {
      client = util.getOpenSocialClient();
    }

    // If the client is null the OpenSocialChooserActivity will be started
    if (client != null) {
      showContacts(client, util.getProvider());
    }
  }

  private void showContacts(OpenSocialClient c, OpenSocialProvider provider) {
    List<OpenSocialPerson> friends = new ArrayList<OpenSocialPerson>();
    try {
      if (provider.isOpenSocial) {
        friends = c.fetchFriends();
      } else {
        OpenSocialBatch batch = new OpenSocialBatch();
        batch.addRequest(new OpenSocialRequest("@me/@all", ""), "friends");
        friends = batch.send(c).getItemAsPersonCollection("friends");
      }
    } catch (Exception e) {
      // TODO: Reduce the number of exceptions the library throws
      Log.i("DisplayFriendActivity", "Couldn't fetch friends from the container: "
          + e.getMessage());
    }

    LinearLayout linearLayout = new LinearLayout(this);
    linearLayout.setLayoutParams(new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.FILL_PARENT,
        LinearLayout.LayoutParams.FILL_PARENT));
    linearLayout.setOrientation(LinearLayout.VERTICAL);


    if (friends.size() > 0) {
      FriendListView contactsView = new FriendListView(this);
      contactsView.setFriends(friends);
      contactsView.setVerticalScrollBarEnabled(true);
      contactsView.setLayoutParams(new ViewGroup.LayoutParams(
          ViewGroup.LayoutParams.FILL_PARENT, 300));
      linearLayout.addView(contactsView);
    } else {
      TextView textView = new TextView(this);
      textView.setText("No contacts found.");
      linearLayout.addView(textView);
    }

    final DisplayFriendsActivity activity = this;

    Button clearAuthButton = new Button(this);
    clearAuthButton.setText("Clear Auth");
    clearAuthButton.setOnClickListener(new View.OnClickListener() {
      public void onClick(View view) {
        util.clearSavedAuthentication();
        client = null;
      }
    });

    Button fetchFriendsButton = new Button(this);
    fetchFriendsButton.setText("Fetch Friends");
    fetchFriendsButton.setOnClickListener(new View.OnClickListener() {
      public void onClick(View view) {
{'fraction_non_alphanumeric': 0.0948349764252036, 'fraction_numerical': 0.002571795970852979, 'mean_word_length': 3.9224683544303796, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '925584', 'n_tokens_mistral': 2632, 'n_tokens_neox': 2478, 'n_words': 720}