/*
Swisscom Safe Connect
Copyright (C) 2014 Swisscom
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.swisscom.safeconnect.view;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.ImageView;
import com.swisscom.safeconnect.R;
import java.util.ArrayList;
import java.util.List;
/**
* Created by vadim on 08.10.14.
* indeterminate imageview-based progress bar
*/
public class PipeProgressBar extends ImageView {
public PipeProgressBar(Context context) {
super(context);
setProgressBar(ConnectionStatusView.VpnState.UNAVAILABLE);
}
public PipeProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
setProgressBar(ConnectionStatusView.VpnState.UNAVAILABLE);
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void initAnimation(int drawableId) {
Bitmap b = BitmapFactory.decodeResource(getResources(), drawableId);
AnimationDrawable shiftedAnimation = getAnimation(b);
if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {
setBackgroundDrawable(shiftedAnimation);
} else {
setBackground(shiftedAnimation);
}
shiftedAnimation.start();
}
private Bitmap getShiftedBitmap(Bitmap bitmap, int shiftX) {
Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
Canvas newBitmapCanvas = new Canvas(newBitmap);
Rect srcRect1 = new Rect(shiftX, 0, bitmap.getWidth(), bitmap.getHeight());
Rect destRect1 = new Rect(srcRect1);
destRect1.offset(-shiftX, 0);
newBitmapCanvas.drawBitmap(bitmap, srcRect1, destRect1, null);
Rect srcRect2 = new Rect(0, 0, shiftX, bitmap.getHeight());
Rect destRect2 = new Rect(srcRect2);
destRect2.offset(bitmap.getWidth() - shiftX, 0);
newBitmapCanvas.drawBitmap(bitmap, srcRect2, destRect2, null);
return newBitmap;
}
private List<Bitmap> getShiftedBitmaps(Bitmap bitmap) {
List<Bitmap> shiftedBitmaps = new ArrayList<Bitmap>();
int fragments = 5;
int shiftLength = bitmap.getWidth() / fragments;
for(int i = 0 ; i < fragments; ++i){
shiftedBitmaps.add(getShiftedBitmap(bitmap, shiftLength * i));
}
return shiftedBitmaps;
}
private AnimationDrawable getAnimation(Bitmap bitmap) {
AnimationDrawable animation = new AnimationDrawable();
animation.setOneShot(false);
List<Bitmap> shiftedBitmaps = getShiftedBitmaps(bitmap);
int duration = 50;
for (int i = shiftedBitmaps.size()-1; i >= 0; i--){
BitmapDrawable navigationBackground = new BitmapDrawable(getResources(),
shiftedBitmaps.get(i));
navigationBackground.setTileModeX(Shader.TileMode.REPEAT);
animation.addFrame(navigationBackground, duration);
}
return animation;
}
public void setProgressBar(ConnectionStatusView.VpnState state) {
switch (state) {
case CONNECTED:
initAnimation(R.drawable.ic_progress_stripe_green);
break;
case DISCONNECTED:
initAnimation(R.drawable.ic_progress_stripe_red);
break;
case WAITING:
break;
case UNAVAILABLE:
initAnimation(R.drawable.ic_progress_stripe_grey);
break;
}
}
}