com.ape.emoji.keyboard.emoji.EmojiKeyboard.java Source code

Java tutorial

Introduction

Here is the source code for com.ape.emoji.keyboard.emoji.EmojiKeyboard.java

Source

/*
 * Copyright 2014 Ankush Sachdeva
 *
 * 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.ape.emoji.keyboard.emoji;

import android.app.Activity;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.LinearInterpolator;
import android.widget.LinearLayout;

import com.ape.emoji.PagerSlidingTabStrip;
import com.ape.emoji.R;
import com.ape.emoji.Screen;
import com.ape.emoji.SmileProcessor;
import com.ape.emoji.keyboard.BaseKeyboard;
import com.ape.emoji.keyboard.emoji.smiles.OnBackspaceClickListener;
import com.ape.emoji.keyboard.emoji.smiles.OnSmileClickListener;
import com.ape.emoji.keyboard.emoji.smiles.RepeatListener;
import com.ape.emoji.keyboard.emoji.smiles.SmilePagerAdapter;
import com.ape.emoji.smiles.SmilesPack;

public class EmojiKeyboard extends BaseKeyboard implements OnSmileClickListener, OnBackspaceClickListener {

    private static final String TAG = "EmojiKeyboard";

    private static final long BINDING_DELAY = 150;
    private SmilePagerAdapter mEmojisAdapter;

    public EmojiKeyboard(Activity activity) {
        super(activity);
    }

    @Override
    public void onEmojiClicked(String smile) {
        if (messageBody == null) {
            return;
        }
        int selectionEnd = messageBody.getSelectionEnd();
        if (selectionEnd < 0) {
            selectionEnd = messageBody.getText().length();
        }
        CharSequence appendString = SmileProcessor.emoji().processEmojiMutable(smile,
                SmileProcessor.CONFIGURATION_BUBBLES);

        messageBody.getText().insert(selectionEnd, appendString);
    }

    @Override
    public void onBackspaceClick(View v) {
        if (messageBody == null) {
            return;
        }
        KeyEvent event = new KeyEvent(0, 0, 0, KeyEvent.KEYCODE_DEL, 0, 0, 0, 0, KeyEvent.KEYCODE_ENDCALL);
        messageBody.dispatchKeyEvent(event);
    }

    @Override
    protected View createView() {
        final View emojiPagerView = LayoutInflater.from(activity).inflate(R.layout.emoji_smiles_pager, null);

        final ViewPager emojiPager = (ViewPager) emojiPagerView.findViewById(R.id.emoji_pager);

        final PagerSlidingTabStrip emojiPagerIndicator = (PagerSlidingTabStrip) emojiPagerView
                .findViewById(R.id.emoji_pager_indicator);
        View backspace = emojiPagerView.findViewById(R.id.backspace);
        final View indicatorContainer = emojiPagerView.findViewById(R.id.indicator_container);

        emojiPagerIndicator.setIndicatorHeight(Screen.dp(activity.getApplicationContext(), 2));
        emojiPagerIndicator.setDividerColor(0x00000000);
        emojiPagerIndicator.setUnderlineHeight(0);
        emojiPagerIndicator.setTabLayoutParams(new LinearLayout.LayoutParams(
                Screen.dp(activity.getApplicationContext(), 48), Screen.dp(activity.getApplicationContext(), 48)));

        backspace.setOnTouchListener(new RepeatListener(500, 100, new OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackspaceClick(v);
            }
        }));

        mEmojisAdapter = new SmilePagerAdapter(this);
        mEmojisAdapter.setTabs(emojiPagerIndicator);
        emojiPager.setAdapter(mEmojisAdapter);
        emojiPagerIndicator.setViewPager(emojiPager);

        //emojiPagerIndicator.setLayoutParams(new RelativeLayout.LayoutParams(Screen.dp(58 * mEmojisAdapter.getCount()), Screen.dp(48)));
        //        emojiPager.postDelayed(new Runnable() {
        //            @Override
        //            public void run() {
        //                emojiPager.setAlpha(0f);
        //                emojiPagerIndicator.setAlpha(0f);
        //                animateView(emojiPager);
        //                animateView(emojiPagerIndicator);
        //                emojiPager.setAdapter(mEmojisAdapter);
        //                emojiPagerIndicator.setViewPager(emojiPager);
        //            }
        //        }, BINDING_DELAY);

        if (SmilesPack.getRecent(activity.getApplicationContext()).size() == 0) {
            emojiPager.setCurrentItem(1);
        }

        return emojiPagerView;
    }

    @Override
    protected void onDismiss() {
        SmileProcessor.emoji().getRecentController().saveRecents();
    }

    void animateView(View view) {
        view.animate().setInterpolator(new LinearInterpolator()).alpha(150).setDuration(300).start();
    }

}