コード例 #1
0
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (APP_ID == null) {
      Util.showAlert(
          this,
          "Warning",
          "Facebook Applicaton ID must be "
              + "specified before running this example: see Example.java");
    }

    setContentView(R.layout.main);
    mLoginButton = (LoginButton) findViewById(R.id.login);
    mText = (TextView) FacebookApplicationActivity.this.findViewById(R.id.txt);
    mUserPic = (ImageView) FacebookApplicationActivity.this.findViewById(R.id.user_pic);
    //       	mFacebook = new Facebook(APP_ID);
    Utility.mFacebook = new Facebook(APP_ID);
    // Instantiate the asynrunner object for asynchronous api calls.
    Utility.mAsyncRunner = new AsyncFacebookRunner(Utility.mFacebook);
    SessionStore.restore(mFacebook, this);
    SessionEvents.addAuthListener(new FbAPIsAuthListener());
    SessionEvents.addLogoutListener(new FbAPIsLogoutListener());
    mLoginButton.init(this, AUTHORIZE_ACTIVITY_RESULT_CODE, Utility.mFacebook, permissions);

    if (Utility.mFacebook.isSessionValid()) {
      requestUserData();
    }

    list = (ListView) findViewById(R.id.main_list);

    list.setOnItemClickListener(this);
    list.setAdapter(new ArrayAdapter<String>(this, R.layout.main_list_item, main_items));
  }
コード例 #2
0
  /**
   * Generate a UI dialog for the request action in the given Android context with the provided
   * parameters.
   *
   * <p>Note that this method is asynchronous and the callback will be invoked in the original
   * calling thread (not in a background thread).
   *
   * @param context The Android context in which we will generate this dialog.
   * @param action String representation of the desired method: e.g. "feed" ...
   * @param parameters String key-value pairs to be passed as URL parameters.
   * @param listener Callback interface to notify the application when the dialog has completed.
   */
  public void dialog(
      Context context, String action, Bundle parameters, final DialogListener listener) {

    String endpoint = DIALOG_BASE_URL + action;
    parameters.putString("display", "touch");
    parameters.putString("redirect_uri", REDIRECT_URI);

    if (action.equals(LOGIN)) {
      parameters.putString("type", "user_agent");
      parameters.putString("client_id", mAppId);
    } else {
      parameters.putString("app_id", mAppId);
    }

    if (isSessionValid()) {
      parameters.putString(TOKEN, getAccessToken());
    }
    String url = endpoint + "?" + Util.encodeUrl(parameters);
    if (context.checkCallingOrSelfPermission(Manifest.permission.INTERNET)
        != PackageManager.PERMISSION_GRANTED) {
      Util.showAlert(context, "Error", "Application requires permission to access the Internet");
    } else {
      new FbDialog(context, url, listener).show();
    }
  }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);

    if (Utility.APP_ID == null) {
      Util.showAlert(
          this,
          "Warning",
          "Facebook Applicaton ID must be "
              + "specified before running this example: see Example.java");
    }

    setContentView(R.layout.main);

    /* Get Handles for all the controls on the screen which needs
     * manipulation
     */
    mLoginButton = (LoginButton) findViewById(R.id.login);
    btnSearchHotel = (Button) findViewById(R.id.btnSearch);
    lblWelcome = (TextView) findViewById(R.id.lblWelcome);
    txtCityName = (EditText) findViewById(R.id.txtCityName);
    layoutForm = (LinearLayout) findViewById(R.id.layoutForm);
    layoutForm.setVisibility(View.INVISIBLE);

    // Create an instance of Facebook object, passing the APP ID.
    Utility.mFacebook = new Facebook(Utility.APP_ID);

    Utility.mAsyncRunner = new AsyncFacebookRunner(Utility.mFacebook);

    SessionStore.restore(Utility.mFacebook, this);
    SessionEvents.addAuthListener(new SampleAuthListener());
    SessionEvents.addLogoutListener(new SampleLogoutListener());
    mLoginButton.init(this, Utility.mFacebook);

    Spinner spin = (Spinner) findViewById(R.id.ddlHotelChain);
    spin.setOnItemSelectedListener(
        new OnItemSelectedListener() {

          public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) {
            // Set the Hotel Chain selected.
            strHotelChain = items_hotel_chain[position];
          }

          public void onNothingSelected(AdapterView<?> arg0) {
            // So, nothing comes here. ;)
          }
        });

    @SuppressWarnings({"rawtypes", "unchecked"})
    ArrayAdapter aa =
        new ArrayAdapter(this, android.R.layout.simple_spinner_item, items_hotel_chain);

    aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spin.setAdapter(aa);

    btnSearchHotel.setOnClickListener(
        new OnClickListener() {

          public void onClick(View v) {
            // If city name is entered, call new intent by passing,
            // city name and hotel chain to the new intent.
            if (!txtCityName.getText().toString().trim().equals("")) {
              Intent i = new Intent(getBaseContext(), HotelList.class);
              i.putExtra("cityName", txtCityName.getText().toString().replace(" ", "+").trim());
              i.putExtra("hotelChain", strHotelChain);
              startActivity(i);
            }
            // City name cannot be empty, so tell the user.
            else {
              Toast.makeText(getBaseContext(), "Please enter city name", Toast.LENGTH_SHORT).show();
            }
          }
        });

    if (Utility.mFacebook.isSessionValid()) {
      lblWelcome.setText("Welcome, " + Utility.strUsername);
      layoutForm.setVisibility(View.VISIBLE);
    }
  }
コード例 #4
0
  @Override
  public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
    switch (position) {
        /*
         * Source Tag: update_status_tag
         * Update user's status by invoking the feed dialog
         * To post to a friend's wall, provide his uid in the 'to' parameter
         * Refer to https://developers.facebook.com/docs/reference/dialogs/feed/ for more info.
         */
      case 0:
        {
          Bundle params = new Bundle();
          params.putString("caption", getString(R.string.app_name));
          params.putString("description", getString(R.string.app_desc));
          params.putString("picture", Utility.HACK_ICON_URL);
          params.putString("name", getString(R.string.app_action));

          Utility.mFacebook.dialog(Hackbook.this, "feed", params, new UpdateStatusListener());
          String access_token = Utility.mFacebook.getAccessToken();
          System.out.println(access_token);
          break;
        }

        /*
         * Source Tag: app_requests
         * Send an app request to friends. If no friend is specified, the user will see a friend selector and will be able to select a maximum of 50 recipients.
         * To send request to specific friend, provide the uid in the 'to' parameter
         * Refer to https://developers.facebook.com/docs/reference/dialogs/requests/ for more info.
         */
      case 1:
        {
          Bundle params = new Bundle();
          params.putString("message", getString(R.string.request_message));
          Utility.mFacebook.dialog(Hackbook.this, "apprequests", params, new AppRequestsListener());
          break;
        }

        /*
         * Source Tag: friends_tag
         * You can get friends using graph.facebook.com/me/friends, this returns the list sorted by UID
         * OR
         * using the friend table. With this you can sort the way you want it.
         * friend table - https://developers.facebook.com/docs/reference/fql/friend/
         * user table - https://developers.facebook.com/docs/reference/fql/user/
         */
      case 2:
        {
          if (!Utility.mFacebook.isSessionValid()) {
            Util.showAlert(this, "Warning", "You must first log in.");
          } else {
            dialog =
                ProgressDialog.show(Hackbook.this, "", getString(R.string.please_wait), true, true);
            new AlertDialog.Builder(this)
                .setTitle(R.string.Graph_FQL_title)
                .setMessage(R.string.Graph_FQL_msg)
                .setPositiveButton(
                    R.string.graph_button,
                    new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int which) {
                        graph_or_fql = "graph";
                        Bundle params = new Bundle();
                        params.putString("fields", "name, picture, location");
                        Utility.mAsyncRunner.request(
                            "me/friends", params, new FriendsRequestListener());
                      }
                    })
                .setNegativeButton(
                    R.string.fql_button,
                    new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int which) {
                        graph_or_fql = "fql";
                        String query =
                            "select name, current_location, uid, pic_square from user where uid in (select uid2 from friend where uid1=me()) order by name";
                        Bundle params = new Bundle();
                        params.putString("method", "fql.query");
                        params.putString("query", query);
                        Utility.mAsyncRunner.request(null, params, new FriendsRequestListener());
                      }
                    })
                .setOnCancelListener(
                    new DialogInterface.OnCancelListener() {
                      @Override
                      public void onCancel(DialogInterface d) {
                        dialog.dismiss();
                      }
                    })
                .show();
          }
          break;
        }

        /*
         * Source Tag: upload_photo
         * You can upload a photo from the media gallery or from a remote server
         * How to upload photo: https://developers.facebook.com/blog/post/498/
         */
      case 3:
        {
          if (!Utility.mFacebook.isSessionValid()) {
            Util.showAlert(this, "Warning", "You must first log in.");
          } else {
            dialog =
                ProgressDialog.show(Hackbook.this, "", getString(R.string.please_wait), true, true);
            new AlertDialog.Builder(this)
                .setTitle(R.string.gallery_remote_title)
                .setMessage(R.string.gallery_remote_msg)
                .setPositiveButton(
                    R.string.gallery_button,
                    new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int which) {
                        Intent intent =
                            new Intent(
                                Intent.ACTION_PICK, (MediaStore.Images.Media.EXTERNAL_CONTENT_URI));
                        startActivityForResult(intent, PICK_EXISTING_PHOTO_RESULT_CODE);
                      }
                    })
                .setNegativeButton(
                    R.string.remote_button,
                    new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int which) {
                        /*
                         * Source tag: upload_photo_tag
                         */
                        Bundle params = new Bundle();
                        params.putString(
                            "url", "http://www.facebook.com/images/devsite/iphone_connect_btn.jpg");
                        params.putString("caption", "FbAPIs Sample App photo upload");
                        Utility.mAsyncRunner.request(
                            "me/photos", params, "POST", new PhotoUploadListener(), null);
                      }
                    })
                .setOnCancelListener(
                    new DialogInterface.OnCancelListener() {
                      @Override
                      public void onCancel(DialogInterface d) {
                        dialog.dismiss();
                      }
                    })
                .show();
          }
          break;
        }

        /*
         * User can check-in to a place, you require publish_checkins permission for that.
         * You can use the default Times Square location or fetch user's current location
         * Get user's checkins - https://developers.facebook.com/docs/reference/api/checkin/
         */
      case 4:
        {
          final Intent myIntent = new Intent(getApplicationContext(), Places.class);

          new AlertDialog.Builder(this)
              .setTitle(R.string.get_location)
              .setMessage(R.string.get_default_or_new_location)
              .setPositiveButton(
                  R.string.current_location_button,
                  new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                      myIntent.putExtra("LOCATION", "current");
                      startActivity(myIntent);
                    }
                  })
              .setNegativeButton(
                  R.string.times_square_button,
                  new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                      myIntent.putExtra("LOCATION", "times_square");
                      startActivity(myIntent);
                    }
                  })
              .show();
          break;
        }

      case 5:
        {
          if (!Utility.mFacebook.isSessionValid()) {
            Util.showAlert(this, "Warning", "You must first log in.");
          } else {
            new FQLQuery(Hackbook.this).show();
          }
          break;
        }
        /*
         * This is advanced feature where you can request new permissions
         * Browser user's graph, his fields and connections.
         * This is similar to the www version - http://developers.facebook.com/tools/explorer/
         */
      case 6:
        {
          Intent myIntent = new Intent(getApplicationContext(), GraphExplorer.class);
          if (Utility.mFacebook.isSessionValid()) {
            Utility.objectID = "me";
          }
          startActivity(myIntent);
          break;
        }
    }
  }
コード例 #5
0
  public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
    switch (position) {
      case 2:
        {
          if (!Utility.mFacebook.isSessionValid()) {
            Util.showAlert(this, "Warning", "You must first log in.");
          } else {
            dialog =
                ProgressDialog.show(
                    FacebookApplicationActivity.this,
                    "",
                    getString(R.string.please_wait),
                    true,
                    true);
            new AlertDialog.Builder(this)
                .setTitle(R.string.Graph_FQL_title)
                .setMessage(R.string.Graph_FQL_msg)
                .setPositiveButton(
                    R.string.graph_button,
                    new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int which) {
                        graph_or_fql = "graph";
                        Bundle params = new Bundle();
                        params.putString("fields", "name, picture, location");
                        Utility.mAsyncRunner.request(
                            "me/friends", params, new FriendsRequestListener());
                      }
                    })
                .setNegativeButton(
                    R.string.fql_button,
                    new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int which) {
                        graph_or_fql = "fql";
                        String query =
                            "select name, current_location, uid, pic_square from user where uid in (select uid2 from friend where uid1=me()) order by name";
                        Bundle params = new Bundle();
                        params.putString("method", "fql.query");
                        params.putString("query", query);
                        Utility.mAsyncRunner.request(null, params, new FriendsRequestListener());
                      }
                    })
                .setOnCancelListener(
                    new DialogInterface.OnCancelListener() {
                      @Override
                      public void onCancel(DialogInterface d) {
                        dialog.dismiss();
                      }
                    })
                .show();
          }
          break;
        }

        /*
         * Source Tag: upload_photo You can upload a photo from the media
         * gallery or from a remote server How to upload photo:
         * https://developers.facebook.com/blog/post/498/
         */
    }
  }
コード例 #6
0
ファイル: FBLogin.java プロジェクト: rakeshnair/openMinds
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.context = this;
    setContentView(R.layout.main);

    // Without application id further processing cannot happen. APP_ID can be obtained from the
    // developers.facebook/apps page.
    if (APP_ID == null) {
      Util.showAlert(context, "Error", "Facebook application id is required");
    }

    // Facebook sesssion initialization
    mFacebook = new Facebook(APP_ID);

    // Check if previously obtained access token exists, in which case use the same.
    mPrefs = getPreferences(MODE_PRIVATE);
    String access_token = mPrefs.getString("access_token", null);
    long expires = mPrefs.getLong("access_expires", 0);
    if (access_token != null) {
      mFacebook.setAccessToken(access_token);
    }
    if (expires != 0) {
      mFacebook.setAccessExpires(expires);
    }

    // Authentication from previously fetched accessed token
    if (mFacebook.isSessionValid()) {
      makeToast("Authorization complete. Values fetched from preferences");
      Bundle params = new Bundle();
      params.putString("fields", "name,id");
      mAsyncRunner = new AsyncFacebookRunner(mFacebook);
      mAsyncRunner.request("me", params, new UserRequestListener());
    }

    // Only call authorize if the access_token has expired.
    if (!mFacebook.isSessionValid()) {
      mFacebook.authorize(
          this,
          PERMISSIONS,
          new DialogListener() {
            @Override
            public void onComplete(Bundle values) {
              SharedPreferences.Editor editor = mPrefs.edit();
              editor.putString("access_token", mFacebook.getAccessToken());
              editor.putLong("access_expires", mFacebook.getAccessExpires());
              editor.commit();

              // Authorization complete after fetching new token from facebook
              makeToast("Authorization complete. Connected to Facebook servers");

              Bundle params = new Bundle();
              params.putString("fields", "name,id");
              mAsyncRunner = new AsyncFacebookRunner(mFacebook);
              mAsyncRunner.request("me", params, new UserRequestListener());
            }

            @Override
            public void onFacebookError(FacebookError error) {
              makeToast(error.getMessage());
            }

            @Override
            public void onError(DialogError e) {
              makeToast(e.getMessage());
            }

            @Override
            public void onCancel() {
              makeToast("Exception in onCancel()");
            }
          });
    }
  }
コード例 #7
0
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (APP_ID == null) {
      Util.showAlert(
          this,
          "Warning",
          "Facebook Applicaton ID must be "
              + "specified before running this example: see FacebookInterfaceActivity.java");
    }

    setContentView(R.layout.facebook);
    mLoginButton = (LoginButton) findViewById(R.id.login);
    mText = (TextView) FacebookInterfaceActivity.this.findViewById(R.id.txt);
    mRequestButton = (Button) findViewById(R.id.requestButton);
    mPostButton = (Button) findViewById(R.id.postButton);
    mDeleteButton = (Button) findViewById(R.id.deletePostButton);
    mUploadButton = (Button) findViewById(R.id.uploadButton);

    mFacebook = new Facebook(APP_ID);
    mAsyncRunner = new AsyncFacebookRunner(mFacebook);

    SessionStore.restore(mFacebook, this);
    SessionEvents.addAuthListener(new SampleAuthListener());
    SessionEvents.addLogoutListener(new SampleLogoutListener());
    mLoginButton.init(this, mFacebook);

    mRequestButton.setOnClickListener(
        new OnClickListener() {
          public void onClick(View v) {
            mAsyncRunner.request("me", new SampleRequestListener());
          }
        });
    mRequestButton.setVisibility(mFacebook.isSessionValid() ? View.VISIBLE : View.INVISIBLE);

    mUploadButton.setOnClickListener(
        new OnClickListener() {
          public void onClick(View v) {
            Bundle params = new Bundle();
            params.putString("method", "photos.upload");

            URL uploadFileUrl = null;
            try {
              uploadFileUrl =
                  new URL("http://www.facebook.com/images/devsite/iphone_connect_btn.jpg");
            } catch (MalformedURLException e) {
              e.printStackTrace();
            }
            try {
              HttpURLConnection conn = (HttpURLConnection) uploadFileUrl.openConnection();
              conn.setDoInput(true);
              conn.connect();
              int length = conn.getContentLength();

              byte[] imgData = new byte[length];
              InputStream is = conn.getInputStream();
              is.read(imgData);
              params.putByteArray("picture", imgData);

            } catch (IOException e) {
              e.printStackTrace();
            }

            mAsyncRunner.request(null, params, "POST", new SampleUploadListener(), null);
          }
        });
    mUploadButton.setVisibility(mFacebook.isSessionValid() ? View.VISIBLE : View.INVISIBLE);

    mPostButton.setOnClickListener(
        new OnClickListener() {
          public void onClick(View v) {
            mFacebook.dialog(FacebookInterfaceActivity.this, "feed", new SampleDialogListener());
          }
        });
    mPostButton.setVisibility(mFacebook.isSessionValid() ? View.VISIBLE : View.INVISIBLE);
  }