Back to project page ShareSDK-for-Android.
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.
/* * 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(); } } }