Android Open Source - ShareSDK-for-Android Auth Page






From Project

Back to project page ShareSDK-for-Android.

License

The source code is released under:

MIT License

If you think the Android project ShareSDK-for-Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Offical Website:http://www.mob.com//from   w  ww  . ja  v  a2 s  .c om
 * Support QQ: 4006852216
 * Offical Wechat Account:ShareSDK   (We will inform you our updated news at the first time by Wechat, if we release a new version. If you get any problem, you can also contact us with Wechat, we will reply you within 24 hours.)
 *
 * Copyright (c) 2013 mob.com. All rights reserved.
 */

package cn.sharesdk.demo;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.ColorDrawable;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckedTextView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;

import cn.sharesdk.framework.CustomPlatform;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.framework.TitleLayout;
import cn.sharesdk.framework.utils.UIHandler;
import cn.sharesdk.onekeyshare.ShareCore;
import m.framework.ui.widget.slidingmenu.SlidingMenu;

/** page to show how to authorize and get user info */
public class AuthPage extends SlidingMenuPage implements
    OnClickListener, PlatformActionListener {
  private View pageView;
  private TitleLayout llTitle;
  private AuthAdapter adapter;

  public AuthPage(SlidingMenu menu) {
    super(menu);
    pageView = getPage();

    llTitle = (TitleLayout) pageView.findViewById(R.id.llTitle);
    llTitle.getBtnBack().setOnClickListener(this);
    llTitle.getTvTitle().setText(R.string.sm_item_auth);

    ListView lvPlats = (ListView) pageView.findViewById(R.id.lvPlats);
    lvPlats.setSelector(new ColorDrawable());
    adapter = new AuthAdapter(this);
    lvPlats.setAdapter(adapter);
    lvPlats.setOnItemClickListener(adapter);
  }

  protected View initPage() {
    return LayoutInflater.from(getContext()).inflate(R.layout.page_auth, null);
  }

  public void onClick(View v) {
    if (v.equals(llTitle.getBtnBack())) {
      if (isMenuShown()) {
        hideMenu();
      }
      else {
        showMenu();
      }
    }
  }

  public void onComplete(Platform plat, int action,
      HashMap<String, Object> res) {
    Message msg = new Message();
    msg.arg1 = 1;
    msg.arg2 = action;
    msg.obj = plat;
    UIHandler.sendMessage(msg, this);
  }

  public void onError(Platform plat, int action, Throwable t) {
    t.printStackTrace();

    Message msg = new Message();
    msg.arg1 = 2;
    msg.arg2 = action;
    msg.obj = plat;
    UIHandler.sendMessage(msg, this);
  }

  public void onCancel(Platform plat, int action) {
    Message msg = new Message();
    msg.arg1 = 3;
    msg.arg2 = action;
    msg.obj = plat;
    UIHandler.sendMessage(msg, this);
  }

  /** handling user info */
  public boolean handleMessage(Message msg) {
    Platform plat = (Platform) msg.obj;
    String text = MainActivity.actionToString(msg.arg2);
    switch (msg.arg1) {
      case 1: {
        // success
        text = plat.getName() + " completed at " + text;
        Toast.makeText(getContext(), text, Toast.LENGTH_SHORT).show();
      }
      break;
      case 2: {
        // failed
        text = plat.getName() + " caught error at " + text;
        Toast.makeText(getContext(), text, Toast.LENGTH_SHORT).show();
        return false;
      }
      case 3: {
        // canceled
        text = plat.getName() + " canceled at " + text;
        Toast.makeText(getContext(), text, Toast.LENGTH_SHORT).show();
        return false;
      }
    }

    adapter.notifyDataSetChanged();
    return false;
  }

  private static class AuthAdapter extends BaseAdapter implements OnItemClickListener {
    private AuthPage page;
    private ArrayList<Platform> platforms;

    public AuthAdapter(AuthPage page) {
      this.page = page;

      // quests platform list
      Platform[] tmp = ShareSDK.getPlatformList();
      platforms = new ArrayList<Platform>();
      if (tmp == null) {
        return;
      }

      for (Platform p : tmp) {
        String name = p.getName();
        if ((p instanceof CustomPlatform)
            || !ShareCore.canGetUserInfo(p.getContext(), name)) {
          continue;
        }
        platforms.add(p);
      }
    }

    public int getCount() {
      return platforms == null ? 0 : platforms.size();
    }

    public Platform getItem(int position) {
      return platforms.get(position);
    }

    public long getItemId(int position) {
      return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
      if (convertView == null) {
        convertView = View.inflate(page.getContext(), R.layout.auth_page_item, null);
      }

      int count = getCount();
      View llItem = convertView.findViewById(R.id.llItem);
      int dp_10 = cn.sharesdk.framework.utils.R.dipToPx(parent.getContext(), 10);
      if (count == 1) {
        llItem.setBackgroundResource(R.drawable.list_item_single_normal);
        llItem.setPadding(0, 0, 0, 0);
        convertView.setPadding(dp_10, dp_10, dp_10, dp_10);
      }
      else if (position == 0) {
        llItem.setBackgroundResource(R.drawable.list_item_first_normal);
        llItem.setPadding(0, 0, 0, 0);
        convertView.setPadding(dp_10, dp_10, dp_10, 0);
      }
      else if (position == count - 1) {
        llItem.setBackgroundResource(R.drawable.list_item_last_normal);
        llItem.setPadding(0, 0, 0, 0);
        convertView.setPadding(dp_10, 0, dp_10, dp_10);
      }
      else {
        llItem.setBackgroundResource(R.drawable.list_item_middle_normal);
        llItem.setPadding(0, 0, 0, 0);
        convertView.setPadding(dp_10, 0, dp_10, 0);
      }

      Platform plat = getItem(position);
      ImageView ivLogo = (ImageView) convertView.findViewById(R.id.ivLogo);
      Bitmap logo = getIcon(plat);
      if (logo != null && !logo.isRecycled()) {
        ivLogo.setImageBitmap(logo);
      }
      CheckedTextView ctvName = (CheckedTextView) convertView.findViewById(R.id.ctvName);
      ctvName.setChecked(plat.isValid());
      if (plat.isValid()) {
        String userName = plat.getDb().get("nickname");
        if (userName == null || userName.length() <= 0 || "null".equals(userName)) {
          userName = getName(plat);
        }
        ctvName.setText(userName);
      } else {
        ctvName.setText(R.string.not_yet_authorized);
      }
      return convertView;
    }

    private Bitmap getIcon(Platform plat) {
      if (plat == null) {
        return null;
      }

      String name = plat.getName();
      if (name == null) {
        return null;
      }

      String resName = "logo_" + plat.getName();
      int resId = cn.sharesdk.framework.utils.R.getResId(R.drawable.class, resName);
      return BitmapFactory.decodeResource(page.getResources(), resId);
    }

    private String getName(Platform plat) {
      if (plat == null) {
        return "";
      }

      String name = plat.getName();
      if (name == null) {
        return "";
      }

      int resId = cn.sharesdk.framework.utils.R.getStringRes(page.getContext(), plat.getName());
      return page.getContext().getString(resId);
    }

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
      Platform plat = getItem(position);
      CheckedTextView ctvName = (CheckedTextView) view.findViewById(R.id.ctvName);
      if (plat == null) {
        ctvName.setChecked(false);
        ctvName.setText(R.string.not_yet_authorized);
        return;
      }

      if (plat.isValid()) {
        plat.removeAccount(true);
        ctvName.setChecked(false);
        ctvName.setText(R.string.not_yet_authorized);
        return;
      }
      //????????SSO???OneKeyShare??????oks.disableSSOWhenAuthorize();?SSO???
      plat.SSOSetting(!CustomShareFieldsPage.getBoolean("enableSSO", true));
      plat.setPlatformActionListener(page);
      plat.authorize();
    }

  }

}




Java Source Code List

cn.sharesdk.demo.AuthPage.java
cn.sharesdk.demo.CustomShareFieldsPage.java
cn.sharesdk.demo.CustomerPage.java
cn.sharesdk.demo.DemoPage.java
cn.sharesdk.demo.GetInforPage.java
cn.sharesdk.demo.GetTokenPage.java
cn.sharesdk.demo.JsonPage.java
cn.sharesdk.demo.Laiwang.java
cn.sharesdk.demo.MainActivity.java
cn.sharesdk.demo.MainAdapter.java
cn.sharesdk.demo.MyAdapter.java
cn.sharesdk.demo.MyQZoneWebShareAdapter.java
cn.sharesdk.demo.OneKeyShareCallback.java
cn.sharesdk.demo.ShareContentCustomizeDemo.java
cn.sharesdk.demo.SlidingMenuPage.java
cn.sharesdk.demo.WechatPage.java
cn.sharesdk.demo.YixinPage.java
cn.sharesdk.demo.wxapi.WXEntryActivity.java
cn.sharesdk.demo.yxapi.YXEntryActivity.java
cn.sharesdk.onekeyshare.CustomerLogo.java
cn.sharesdk.onekeyshare.CustomerLogo.java
cn.sharesdk.onekeyshare.EditPageFakeActivity.java
cn.sharesdk.onekeyshare.EditPageFakeActivity.java
cn.sharesdk.onekeyshare.FollowerListFakeActivity.java
cn.sharesdk.onekeyshare.FollowerListFakeActivity.java
cn.sharesdk.onekeyshare.OnekeyShareTheme.java
cn.sharesdk.onekeyshare.OnekeyShareTheme.java
cn.sharesdk.onekeyshare.OnekeyShare.java
cn.sharesdk.onekeyshare.OnekeyShare.java
cn.sharesdk.onekeyshare.PicViewer.java
cn.sharesdk.onekeyshare.PicViewer.java
cn.sharesdk.onekeyshare.PlatformListFakeActivity.java
cn.sharesdk.onekeyshare.PlatformListFakeActivity.java
cn.sharesdk.onekeyshare.Shake2Share.java
cn.sharesdk.onekeyshare.Shake2Share.java
cn.sharesdk.onekeyshare.ShareContentCustomizeCallback.java
cn.sharesdk.onekeyshare.ShareContentCustomizeCallback.java
cn.sharesdk.onekeyshare.ShareCore.java
cn.sharesdk.onekeyshare.ShareCore.java
cn.sharesdk.onekeyshare.ThemeShareCallback.java
cn.sharesdk.onekeyshare.ThemeShareCallback.java
cn.sharesdk.onekeyshare.theme.classic.EditPage.java
cn.sharesdk.onekeyshare.theme.classic.EditPage.java
cn.sharesdk.onekeyshare.theme.classic.FollowListPage.java
cn.sharesdk.onekeyshare.theme.classic.FollowListPage.java
cn.sharesdk.onekeyshare.theme.classic.PlatformGridView.java
cn.sharesdk.onekeyshare.theme.classic.PlatformGridView.java
cn.sharesdk.onekeyshare.theme.classic.PlatformListPage.java
cn.sharesdk.onekeyshare.theme.classic.PlatformListPage.java
cn.sharesdk.onekeyshare.theme.skyblue.EditPage.java
cn.sharesdk.onekeyshare.theme.skyblue.EditPage.java
cn.sharesdk.onekeyshare.theme.skyblue.FollowListPage.java
cn.sharesdk.onekeyshare.theme.skyblue.FollowListPage.java
cn.sharesdk.onekeyshare.theme.skyblue.PlatformGridViewAdapter.java
cn.sharesdk.onekeyshare.theme.skyblue.PlatformGridViewAdapter.java
cn.sharesdk.onekeyshare.theme.skyblue.PlatformListPage.java
cn.sharesdk.onekeyshare.theme.skyblue.PlatformListPage.java
cn.sharesdk.socialization.sample.MainActivity.java
cn.sharesdk.socialization.sample.MyPlatform.java
cn.sharesdk.socialization.sample.wxapi.WXEntryActivity.java
cn.sharesdk.socialization.sample.yxapi.YXEntryActivity.java