Skip to content

Commit

Permalink
Add sound to buttons
Browse files Browse the repository at this point in the history
+ document changes as requested by Apache license 2.0
  • Loading branch information
Raduh committed Jul 22, 2014
1 parent 38b95d9 commit 2c8842e
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 154 deletions.
5 changes: 5 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ can be found at:

http://www.apache.org/licenses/LICENSE-2.0

Changes made to the original project:
- Simplified project, and removed all indicators except CircleIndicator
- Removed numerous methods from CircleIndicator
- added an AudioManager field to the CircleIndicator

--------------------------------------------------------------------------------
*All graphic assets are licensed by TobyRich GmbH under the
Creative Commons Attribution-NonCommercial 4.0 International License. This
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ public void run() {

@Override
public void didUpdateBatteryLevel(float percent) {
Log.i(TAG, "did update battery level");
final float R_batt = 0.520f; // Ohm
/* 0.5 Amps is the current through the motor at MAX_MOTOR_SPEED */
final float I_motor = (planeState.getAdjustedMotorSpeed() / Const.MAX_MOTOR_SPEED) * 0.5f; // Amps
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
Expand All @@ -43,6 +44,7 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.SoundEffectConstants;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
Expand Down Expand Up @@ -81,6 +83,8 @@ public class FullscreenActivity extends Activity {
private GestureDetector gestureDetector; // touch events
private PlaneState planeState; // singleton with variables used app-wide

AudioManager audioManager ;

@Override
public void onResume() {
super.onResume();
Expand All @@ -107,13 +111,16 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);

audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);

// Instantiate a ViewPager and a PagerAdapter
ViewPager screenPager = (ViewPager) findViewById(R.id.screenPager);
screenPager.setAdapter(new ScreenSlideAdapter());

CirclePageIndicator screenIndicator =
(CirclePageIndicator) findViewById(R.id.screenIndicator);
screenIndicator.setViewPager(screenPager);
screenIndicator.setAudioManager(audioManager);

screenPager.setCurrentItem(1); // horizon screen
screenPager.setOffscreenPageLimit(2);
Expand Down Expand Up @@ -232,6 +239,7 @@ public void onClick(View v) {
}

public void initializeSettingsScreen() {
final float FX_VOLUME = 10.0f;
/* setting the version data at the bottom of the screen */
String appVersion = "uknown";
try {
Expand All @@ -249,6 +257,7 @@ public void initializeSettingsScreen() {
rudderReverse.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
audioManager.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD, FX_VOLUME);
planeState.rudderReversed = isChecked;
}
});
Expand All @@ -257,6 +266,7 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
flAssistSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
audioManager.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD, FX_VOLUME);
planeState.enableFlightAssist(isChecked);
}
});
Expand All @@ -267,6 +277,7 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
towerSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
audioManager.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD, FX_VOLUME);
ImageView atcOn = (ImageView) findViewById(R.id.atcOn);
ImageView atcOff = (ImageView) findViewById(R.id.atcOff);

Expand Down
6 changes: 4 additions & 2 deletions SmartPlane/src/main/res/layout/plane_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
android:id="@+id/rudderSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
android:layout_alignParentRight="true"
android:soundEffectsEnabled="true"/>
</RelativeLayout>

<View
Expand Down Expand Up @@ -69,7 +70,8 @@
android:id="@+id/flAssistSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
android:layout_alignParentRight="true"
android:soundEffectsEnabled="true"/>
</RelativeLayout>

<View
Expand Down
1 change: 1 addition & 0 deletions SmartPlane/src/main/res/layout/weather_center.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
android:textColor="@color/dark_green"
android:layout_marginTop="40dp"
android:lineSpacingMultiplier="1.5"
android:textAppearance="?android:attr/textAppearanceSmall"
android:visibility="gone" />

<ProgressBar
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
* 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.
*
* Changes made to the original project:
- Simplified project, and removed all indicators except CircleIndicator
- Removed numerous methods from CircleIndicator
- added an AudioManager field to the CircleIndicator
*/
package com.viewpagerindicator;

Expand All @@ -23,46 +28,35 @@
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.drawable.Drawable;
import android.media.AudioManager;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.v4.view.MotionEventCompat;
import android.support.v4.view.ViewConfigurationCompat;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;

import static android.graphics.Paint.ANTI_ALIAS_FLAG;
import static android.widget.LinearLayout.HORIZONTAL;
import static android.widget.LinearLayout.VERTICAL;

/**
* Draws circles (one for each view). The current view position is filled and
* others are only stroked.
*/
@SuppressWarnings("UnusedDeclaration")
public class CirclePageIndicator extends View implements PageIndicator {
private static final int INVALID_POINTER = -1;

private float mRadius;
private final Paint mPaintPageFill = new Paint(ANTI_ALIAS_FLAG);
private final Paint mPaintStroke = new Paint(ANTI_ALIAS_FLAG);
private final Paint mPaintFill = new Paint(ANTI_ALIAS_FLAG);
private ViewPager mViewPager;
private ViewPager.OnPageChangeListener mListener;
private int mCurrentPage;
private int mSnapPage;
private float mPageOffset;
private int mScrollState;
private int mOrientation;
private boolean mCentered;
private boolean mSnap;

private int mTouchSlop;
private float mLastMotionX = -1;
private int mActivePointerId = INVALID_POINTER;
private boolean mIsDragging;

private AudioManager mAudioManager;

public CirclePageIndicator(Context context) {
this(context, null);
Expand All @@ -85,7 +79,6 @@ public CirclePageIndicator(Context context, AttributeSet attrs, int defStyle) {
final float defaultStrokeWidth = res.getDimension(R.dimen.default_circle_indicator_stroke_width);
final float defaultRadius = res.getDimension(R.dimen.default_circle_indicator_radius);
final boolean defaultCentered = res.getBoolean(R.bool.default_circle_indicator_centered);
final boolean defaultSnap = res.getBoolean(R.bool.default_circle_indicator_snap);

//Retrieve styles attributes
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CirclePageIndicator, defStyle, 0);
Expand All @@ -100,98 +93,13 @@ public CirclePageIndicator(Context context, AttributeSet attrs, int defStyle) {
mPaintFill.setStyle(Style.FILL);
mPaintFill.setColor(a.getColor(R.styleable.CirclePageIndicator_fillColor, defaultFillColor));
mRadius = a.getDimension(R.styleable.CirclePageIndicator_radius, defaultRadius);
mSnap = a.getBoolean(R.styleable.CirclePageIndicator_snap, defaultSnap);

Drawable background = a.getDrawable(R.styleable.CirclePageIndicator_android_background);
if (background != null) {
setBackground(background);
setBackground(background);
}

a.recycle();

final ViewConfiguration configuration = ViewConfiguration.get(context);
mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(configuration);
}


public void setCentered(boolean centered) {
mCentered = centered;
invalidate();
}

public boolean isCentered() {
return mCentered;
}

public void setPageColor(int pageColor) {
mPaintPageFill.setColor(pageColor);
invalidate();
}

public int getPageColor() {
return mPaintPageFill.getColor();
}

public void setFillColor(int fillColor) {
mPaintFill.setColor(fillColor);
invalidate();
}

public int getFillColor() {
return mPaintFill.getColor();
}

public void setOrientation(int orientation) {
switch (orientation) {
case HORIZONTAL:
case VERTICAL:
mOrientation = orientation;
requestLayout();
break;

default:
throw new IllegalArgumentException("Orientation must be either HORIZONTAL or VERTICAL.");
}
}

public int getOrientation() {
return mOrientation;
}

public void setStrokeColor(int strokeColor) {
mPaintStroke.setColor(strokeColor);
invalidate();
}

public int getStrokeColor() {
return mPaintStroke.getColor();
}

public void setStrokeWidth(float strokeWidth) {
mPaintStroke.setStrokeWidth(strokeWidth);
invalidate();
}

public float getStrokeWidth() {
return mPaintStroke.getStrokeWidth();
}

public void setRadius(float radius) {
mRadius = radius;
invalidate();
}

public float getRadius() {
return mRadius;
}

public void setSnap(boolean snap) {
mSnap = snap;
invalidate();
}

public boolean isSnap() {
return mSnap;
}

@Override
Expand Down Expand Up @@ -264,10 +172,8 @@ protected void onDraw(Canvas canvas) {
}

//Draw the filled circle according to the current scroll
float cx = (mSnap ? mSnapPage : mCurrentPage) * threeRadius;
if (!mSnap) {
cx += mPageOffset * threeRadius;
}
float cx = (mCurrentPage + mPageOffset) * threeRadius;

if (mOrientation == HORIZONTAL) {
dX = longOffset + cx;
dY = shortOffset;
Expand Down Expand Up @@ -337,9 +243,13 @@ public void onPageScrolled(int position, float positionOffset, int positionOffse

@Override
public void onPageSelected(int position) {
if (mSnap || mScrollState == ViewPager.SCROLL_STATE_IDLE) {
if (mAudioManager != null) {
final float FX_VOLUME = 10.0f;
/* Disable for now, until we find a better sound */
// mAudioManager.playSoundEffect(EFFECT, FX_VOLUME);
}
if (mScrollState == ViewPager.SCROLL_STATE_IDLE) {
mCurrentPage = position;
mSnapPage = position;
invalidate();
}

Expand Down Expand Up @@ -370,8 +280,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
/**
* Determines the width of this view
*
* @param measureSpec
* A measureSpec packed into an int
* @param measureSpec A measureSpec packed into an int
* @return The width of the view, honoring constraints from measureSpec
*/
private int measureLong(int measureSpec) {
Expand All @@ -385,7 +294,7 @@ private int measureLong(int measureSpec) {
} else {
//Calculate the width according the views count
final int count = mViewPager.getAdapter().getCount();
result = (int)(getPaddingLeft() + getPaddingRight()
result = (int) (getPaddingLeft() + getPaddingRight()
+ (count * 2 * mRadius) + (count - 1) * mRadius + 1);
//Respect AT_MOST value if that was what is called for by measureSpec
if (specMode == MeasureSpec.AT_MOST) {
Expand All @@ -398,8 +307,7 @@ private int measureLong(int measureSpec) {
/**
* Determines the height of this view
*
* @param measureSpec
* A measureSpec packed into an int
* @param measureSpec A measureSpec packed into an int
* @return The height of the view, honoring constraints from measureSpec
*/
private int measureShort(int measureSpec) {
Expand All @@ -412,7 +320,7 @@ private int measureShort(int measureSpec) {
result = specSize;
} else {
//Measure the height
result = (int)(2 * mRadius + getPaddingTop() + getPaddingBottom() + 1);
result = (int) (2 * mRadius + getPaddingTop() + getPaddingBottom() + 1);
//Respect AT_MOST value if that was what is called for by measureSpec
if (specMode == MeasureSpec.AT_MOST) {
result = Math.min(result, specSize);
Expand All @@ -423,10 +331,9 @@ private int measureShort(int measureSpec) {

@Override
public void onRestoreInstanceState(Parcelable state) {
SavedState savedState = (SavedState)state;
SavedState savedState = (SavedState) state;
super.onRestoreInstanceState(savedState.getSuperState());
mCurrentPage = savedState.currentPage;
mSnapPage = savedState.currentPage;
requestLayout();
}

Expand All @@ -450,6 +357,7 @@ private SavedState(Parcel in) {
currentPage = in.readInt();
}

@SuppressWarnings("NullableProblems")
@Override
public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
Expand All @@ -469,4 +377,8 @@ public SavedState[] newArray(int size) {
}
};
}

public void setAudioManager(AudioManager audioManager) {
mAudioManager = audioManager;
}
}
Loading

0 comments on commit 2c8842e

Please sign in to comment.