Android Open Source - api-ai-android-sdk Sound Level Button






From Project

Back to project page api-ai-android-sdk.

License

The source code is released under:

Apache License

If you think the Android project api-ai-android-sdk 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

package ai.api.ui;
/*  ww w .j  av  a2 s  .  com*/
/***********************************************************************************************************************
 *
 * API.AI Android SDK - client-side libraries for API.AI
 * =================================================
 *
 * Copyright (C) 2014 by Speaktoit, Inc. (https://www.speaktoit.com)
 * https://www.api.ai
 *
 ***********************************************************************************************************************
 *
 * 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.
 *
 ***********************************************************************************************************************/

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import ai.api.R;

public class SoundLevelButton extends MaskedColorView {

    private static final String TAG = SoundLevelButton.class.getName();

    protected static final int[] STATE_LISTENING = new int[] { R.attr.state_listening };

    private final SoundLevelCircleDrawable backgroundDrawable;
    protected boolean listening = false;

    @SuppressWarnings("UnusedDeclaration")
    public SoundLevelButton(@NonNull final Context context) {
        super(context);
        backgroundDrawable = new SoundLevelCircleDrawable(getParams(context, null));
        setCircleBackground(backgroundDrawable);
        init();
    }

    @SuppressWarnings("UnusedDeclaration")
    public SoundLevelButton(@NonNull final Context context, @Nullable final AttributeSet attrs) {
        super(context, attrs);
        backgroundDrawable = new SoundLevelCircleDrawable(getParams(context, attrs));
        setCircleBackground(backgroundDrawable);
        init();
    }

    @SuppressWarnings("UnusedDeclaration")
    public SoundLevelButton(@NonNull final Context context, @Nullable final AttributeSet attrs, final int defStyle) {
        super(context, attrs, defStyle);
        backgroundDrawable = new SoundLevelCircleDrawable(getParams(context, attrs));
        setCircleBackground(backgroundDrawable);
        init();
    }

    private void init() {
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(final View v) {
                SoundLevelButton.this.onClick(v);
            }
        });
    }

    protected void onClick(final View v) {

    }

    @Nullable
    private SoundLevelCircleDrawable.Params getParams(@NonNull final Context context, @Nullable final AttributeSet attrs) {
        if (attrs != null) {
            final TypedArray viewAttrs = context.obtainStyledAttributes(attrs, R.styleable.SoundLevelButton);

            try {
                this.listening = viewAttrs.getBoolean(R.styleable.SoundLevelButton_state_listening, false);

                final float maxRadius = viewAttrs.getDimension(R.styleable.SoundLevelButton_maxRadius, -1);
                final float minRadius = viewAttrs.getDimension(R.styleable.SoundLevelButton_minRadius, -1);
                final float circleCenterX = viewAttrs.getDimension(R.styleable.SoundLevelButton_circleCenterX, -1);
                final float circleCenterY = viewAttrs.getDimension(R.styleable.SoundLevelButton_circleCenterY, -1);
                final int centerColor = viewAttrs.getColor(R.styleable.SoundLevelButton_centerColor, SoundLevelCircleDrawable.CENTER_COLOR_DEF);
                final int haloColor = viewAttrs.getColor(R.styleable.SoundLevelButton_haloColor, SoundLevelCircleDrawable.HALO_COLOR_DEF);
                return new SoundLevelCircleDrawable.Params(maxRadius, minRadius, circleCenterX, circleCenterY, centerColor, haloColor);
            } finally {
                viewAttrs.recycle();
            }
        }
        return null;
    }

    @Override
    public int[] onCreateDrawableState(final int extraSpace) {
        final int[] state = super.onCreateDrawableState(extraSpace + 1);
        if (listening) mergeDrawableStates(state, STATE_LISTENING);
        return state;
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void setCircleBackground(final SoundLevelCircleDrawable soundLevelCircleDrawable) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN)
            //noinspection deprecation
            setBackgroundDrawable(soundLevelCircleDrawable);
        else
            setBackground(soundLevelCircleDrawable);
    }

    public void setDrawSoundLevel(final boolean drawSoundLevel) {
        listening = drawSoundLevel;
        backgroundDrawable.setDrawSoundLevel(drawSoundLevel);
        refreshDrawableState();
        postInvalidate();
    }

    protected void setDrawCenter(final boolean drawCenter) {
        this.backgroundDrawable.setDrawCenter(drawCenter);
    }

    public void setSoundLevel(final float soundLevel) {
        backgroundDrawable.setSoundLevel(soundLevel);
        postInvalidate();
    }

    @Override
    protected String getDebugState() {
        return super.getDebugState() + "\ndrawSL: " + listening;
    }

    protected float getMinRadius() {
        return backgroundDrawable.getMinRadius();
    }
}




Java Source Code List

ai.api.AIConfiguration.java
ai.api.AIDataService.java
ai.api.AIListener.java
ai.api.AIServiceException.java
ai.api.AIService.java
ai.api.AudioRequestTask.java
ai.api.AudioRequest.java
ai.api.GoogleRecognitionServiceImpl.java
ai.api.GsonFactory.java
ai.api.RecognitionEngine.java
ai.api.RequestTask.java
ai.api.SpeaktoitRecognitionServiceImpl.java
ai.api.http.HttpClient.java
ai.api.model.AIContext.java
ai.api.model.AIError.java
ai.api.model.AIRequest.java
ai.api.model.AIResponse.java
ai.api.model.Constants.java
ai.api.model.Metadata.java
ai.api.model.QuestionMetadata.java
ai.api.model.Result.java
ai.api.model.Status.java
ai.api.sample.AIButtonSampleActivity.java
ai.api.sample.AIServiceSampleActivity.java
ai.api.sample.Config.java
ai.api.sample.MainActivity.java
ai.api.ui.AIButton.java
ai.api.ui.MaskedColorView.java
ai.api.ui.SoundLevelButton.java
ai.api.ui.SoundLevelCircleDrawable.java
ai.api.util.RecognizerChecker.java
ai.api.util.VoiceActivityDetector.java