PageRenderTime 22ms CodeModel.GetById 14ms app.highlight 5ms RepoModel.GetById 2ms app.codeStats 0ms

/TalkBack/src/com/google/android/marvin/talkback/PlayableListPreference.java

http://eyes-free.googlecode.com/
Java | 113 lines | 71 code | 20 blank | 22 comment | 12 complexity | 22441bec3fd20a79290f649720cdb050 MD5 | raw file
  1/*
  2 * Copyright 2011 Google Inc.
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5 * use this file except in compliance with the License. You may obtain a copy of
  6 * the License at
  7 *
  8 * http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 13 * License for the specific language governing permissions and limitations under
 14 * the License.
 15 */
 16
 17package com.google.android.marvin.talkback;
 18
 19import android.content.Context;
 20import android.content.res.Resources;
 21import android.preference.ListPreference;
 22import android.util.AttributeSet;
 23import android.util.Log;
 24import android.view.View;
 25import android.view.View.AccessibilityDelegate;
 26import android.view.View.OnClickListener;
 27import android.view.accessibility.AccessibilityEvent;
 28import android.widget.ImageView;
 29
 30/**
 31 * Extends {@link ListPreference} by providing a preview button for sounds and
 32 * vibration patterns.
 33 * 
 34 * @author alanv@google.com (Alan Viverette)
 35 */
 36public class PlayableListPreference extends ListPreference {
 37    private static final String TAG = PlayableListPreference.class.getSimpleName();
 38
 39    private static final String TYPE_SOUND = "raw/";
 40    private static final String TYPE_VIBRATION = "array/";
 41
 42    private final FeedbackController mFeedbackController;
 43
 44    public PlayableListPreference(Context context) {
 45        this(context, null);
 46    }
 47
 48    public PlayableListPreference(Context context, AttributeSet attrs) {
 49        super(context, attrs);
 50
 51        setWidgetLayoutResource(R.layout.play_button);
 52
 53        mFeedbackController = FeedbackController.getInstance(context);
 54    }
 55
 56    @Override
 57    protected void onBindView(View view) {
 58        final ImageView playButton = (ImageView) view.findViewById(R.id.play_button);
 59
 60        if (playButton != null) {
 61            playButton.setOnClickListener(mClickListener);
 62            playButton.setAccessibilityDelegate(mDelegate);
 63        }
 64
 65        super.onBindView(view);
 66    }
 67
 68    private void playCurrentValue() {
 69        final String value = getValue();
 70
 71        if (value == null || !value.matches("\\w+/\\w+")) {
 72            Log.e(TAG, "Invalid resource format: " + value);
 73            return;
 74        }
 75
 76        final Context context = getContext();
 77        final Resources res = context.getResources();
 78        final int resId = res.getIdentifier(value, null, context.getPackageName());
 79
 80        if (resId <= 0) {
 81            Log.e(TAG, "Unable to load resource identifier: " + value);
 82            return;
 83        }
 84
 85        if (value.startsWith(TYPE_SOUND)) {
 86            mFeedbackController.playSound(resId, 1.0f);
 87        } else if (value.startsWith(TYPE_VIBRATION)) {
 88            mFeedbackController.playVibration(resId);
 89        } else {
 90            Log.e(TAG, "Unknown resource type: " + value);
 91            return;
 92        }
 93    }
 94
 95    private final AccessibilityDelegate mDelegate = new AccessibilityDelegate() {
 96        @Override
 97        public void sendAccessibilityEvent(View host, int eventType) {
 98            // Don't send VIEW_CLICKED events.
 99            if (eventType == AccessibilityEvent.TYPE_VIEW_CLICKED) {
100                return;
101            }
102
103            super.sendAccessibilityEvent(host, eventType);
104        }
105    };
106
107    private final OnClickListener mClickListener = new OnClickListener() {
108        @Override
109        public void onClick(View v) {
110            playCurrentValue();
111        }
112    };
113}