/** * Copyright 2014 Daum Kakao Corp. * * Redistribution and modification in source or binary forms are not permitted without specific prior written permission.  * * 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 com.kakao.widget; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.util.AttributeSet; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.FrameLayout; import android.widget.ListAdapter; import android.widget.TextView; import com.kakao.AuthType; import com.kakao.Session; import com.kakao.helper.StoryProtocol; import com.kakao.helper.TalkProtocol; import com.kakao.sdk.R; /** * 로그인 버튼 * </br> * 로그인 layout에 {@link com.kakao.widget.LoginButton}을 선언하여 사용한다. * @author MJ */ public class LoginButton extends FrameLayout { public LoginButton(Context context) { super(context); } public LoginButton(Context context, AttributeSet attrs) { super(context, attrs); } public LoginButton(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } /** * 로그인 버튼 클릭시 세션을 오픈하도록 설정한다. */ @Override protected void onAttachedToWindow() { super.onAttachedToWindow(); inflate(getContext(), R.layout.kakao_login_layout, this); setOnClickListener(new OnClickListener() { public void onClick(View view) { // 카톡 또는 카스가 존재하면 옵션을 보여주고, 존재하지 않으면 바로 직접 로그인창. final List<AuthType> authTypes = getAuthTypes(); if(authTypes.size() == 1){ Session.getCurrentSession().open(authTypes.get(0)); } else { onClickLoginButton(authTypes); } } }); } private List<AuthType> getAuthTypes() { final List<AuthType> availableAuthTypes = new ArrayList<AuthType>(); if(TalkProtocol.existCapriLoginActivityInTalk(getContext())){ availableAuthTypes.add(AuthType.KAKAO_TALK); } if(StoryProtocol.existCapriLoginActivityInStory(getContext())){ availableAuthTypes.add(AuthType.KAKAO_STORY); } availableAuthTypes.add(AuthType.KAKAO_ACCOUNT); final AuthType[] selectedAuthTypes = Session.getCurrentSession().getAuthTypes(); availableAuthTypes.retainAll(Arrays.asList(selectedAuthTypes)); // 개발자가 설정한 것과 available 한 타입이 없다면 직접계정 입력이 뜨도록 한다. if(availableAuthTypes.size() == 0){ availableAuthTypes.add(AuthType.KAKAO_ACCOUNT); } return availableAuthTypes; } private void onClickLoginButton(final List<AuthType> authTypes){ final List<Item> itemList = new ArrayList<Item>(); if(authTypes.contains(AuthType.KAKAO_TALK)) { itemList.add(new Item(R.string.com_kakao_kakaotalk_account, R.drawable.kakaotalk_icon, AuthType.KAKAO_TALK)); } if(authTypes.contains(AuthType.KAKAO_STORY)) { itemList.add(new Item(R.string.com_kakao_kakaostory_account, R.drawable.kakaostory_icon, AuthType.KAKAO_STORY)); } if(authTypes.contains(AuthType.KAKAO_ACCOUNT)){ itemList.add(new Item(R.string.com_kakao_other_kakaoaccount, R.drawable.kakaoaccount_icon, AuthType.KAKAO_ACCOUNT)); } itemList.add(new Item(R.string.com_kakao_account_cancel, 0, null)); //no icon for this one final Item[] items = itemList.toArray(new Item[itemList.size()]); final ListAdapter adapter = new ArrayAdapter<Item>( getContext(), android.R.layout.select_dialog_item, android.R.id.text1, items){ public View getView(int position, View convertView, ViewGroup parent) { View v = super.getView(position, convertView, parent); TextView tv = (TextView)v.findViewById(android.R.id.text1); tv.setText(items[position].textId); tv.setTextSize(15); tv.setGravity(Gravity.CENTER); if(position == itemList.size() -1) { tv.setBackgroundResource(R.drawable.kakao_cancel_button_background); } else { tv.setBackgroundResource(R.drawable.kakao_account_button_background); } tv.setCompoundDrawablesWithIntrinsicBounds(items[position].icon, 0, 0, 0); int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f); tv.setCompoundDrawablePadding(dp5); return v; } }; new AlertDialog.Builder(getContext()) .setAdapter(adapter, new DialogInterface.OnClickListener() { public void onClick(final DialogInterface dialog, final int position) { final AuthType authType = items[position].authType; if(authType == null){ dialog.dismiss(); } else{ Session.getCurrentSession().open(authType); } } }).create().show(); } private static class Item { public final int textId; public final int icon; public final AuthType authType; public Item(final int textId, final Integer icon, final AuthType authType) { this.textId = textId; this.icon = icon; this.authType = authType; } } }