i want change my overlayedButton (always on top) to overlayedLayout and in this layout put instlled app icon for lunch.
i do not want a widget layout. i want a layout like widget that every where is showing and always on top
this image...
my HUD class
public class HUD extends Service implements OnTouchListener, View.OnClickListener {
private View topLeftView;
private Button overlayedButton,overlayedButton2;
private float offsetX;
private float offsetY;
private int originalXPos;
private int originalYPos;
private boolean moving;
private WindowManager wm;
private String TAG = "GET_APP";
private LayoutInflater li;
private ViewGroup myview;
private int deviceWidth;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
overlayedButton = new Button(this);
overlayedButton.setBackgroundResource(R.drawable.main);
overlayedButton.setOnTouchListener(this);
overlayedButton.setAlpha(0.0f);
overlayedButton.setBottom(100);
overlayedButton.setOnClickListener(this);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.LEFT | Gravity.TOP;
params.x = 0;
params.y = 0;
wm.addView(overlayedButton, params);
topLeftView = new View(this);
WindowManager.LayoutParams topLeftParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.TRANSLUCENT);
topLeftParams.gravity = Gravity.LEFT | Gravity.TOP;
topLeftParams.x = 0;
topLeftParams.y = 0;
topLeftParams.width = 0;
topLeftParams.height = 0;
wm.addView(topLeftView, topLeftParams);
}
#Override
public void onDestroy() {
super.onDestroy();
if (overlayedButton != null) {
wm.removeView(overlayedButton);
wm.removeView(topLeftView);
overlayedButton = null;
topLeftView = null;
}
}
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
float x = event.getRawX();
float y = event.getRawY();
moving = false;
int[] location = new int[2];
overlayedButton.getLocationOnScreen(location);
originalXPos = location[0];
originalYPos = location[1];
offsetX = originalXPos - x;
offsetY = originalYPos - y;
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
int[] topLeftLocationOnScreen = new int[2];
topLeftView.getLocationOnScreen(topLeftLocationOnScreen);
System.out.println("topLeftY="+topLeftLocationOnScreen[1]);
System.out.println("originalY="+originalYPos);
float x = event.getRawX();
float y = event.getRawY();
WindowManager.LayoutParams params = (WindowManager.LayoutParams) overlayedButton.getLayoutParams();
int newX = (int) (offsetX + x);
int newY = (int) (offsetY + y);
if (Math.abs(newX - originalXPos) < 1 && Math.abs(newY - originalYPos) < 1 && !moving) {
return false;
}
params.x = newX - (topLeftLocationOnScreen[0]);
params.y = newY - (topLeftLocationOnScreen[1]);
wm.updateViewLayout(overlayedButton, params);
moving = true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
if (moving) {
return true;
}
}
return false;
}
#Override
public void onClick(View v) {
Toast.makeText(this, "Overlay button click event", Toast.LENGTH_SHORT).show();
Intent dialogIntent = new Intent(this, ArcMenuActivity.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(dialogIntent);
}
}
ArcMenuActivity
public class ArcMenuActivity extends Activity {
private int state = 0;
public static final String FAVORITES = "Product_Favorite";
private ArrayList<Intent> Ar;
private Drawable icon;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
state = 1;
switch (state){
case 1:{
setContentView(R.layout.activity_ray);
final RayMenu rayMenu = (RayMenu) findViewById(R.id.ray_menu);
Ar = getFavorites(getApplicationContext());
final int itemCount = Ar.size();
for (int i = 0; i < itemCount; i++) {
ImageView item = new ImageView(this);
try {
icon = getPackageManager().getApplicationIcon(Ar.get(i).getPackage());
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
item.setImageDrawable(icon);
final int position = i;
rayMenu.addItem(item, new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(ArcMenuActivity.this, "position:" + position, Toast.LENGTH_SHORT).show();
startActivity(Ar.get(position));
finish();
}
});
}
break;}
case 2:{
break;}
case 3:{
break;}
}
Toast.makeText(getBaseContext(),"onCreate", Toast.LENGTH_LONG).show();
Intent svc = new Intent(this, HUD.class);
startService(svc);
}
public ArrayList<Intent> getFavorites(Context context) {
SharedPreferences settings;
List<Intent> favorites;
settings = context.getSharedPreferences("INTENT",
Context.MODE_PRIVATE);
if (settings.contains(FAVORITES)) {
String jsonFavorites = settings.getString(FAVORITES, null);
Gson gson = new Gson();
Intent[] favoriteItems = gson.fromJson(jsonFavorites,
Intent[].class);
favorites = Arrays.asList(favoriteItems);
favorites = new ArrayList<Intent>(favorites);
} else
return null;
return (ArrayList<Intent>) favorites;
}
private void initArcMenu(ArcMenu menu, int[] itemDrawables) {
final int itemCount = itemDrawables.length;
for (int i = 0; i < itemCount; i++) {
ImageView item = new ImageView(this);
item.setImageResource(itemDrawables[i]);
final int position = i;
menu.addItem(item, new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(ArcMenuActivity.this, "position:" + position, Toast.LENGTH_SHORT).show();
}
});
}
}
}
}
Related
I have an incomming call Broadcast receiver, it is working on android 7.1 nut not on android 4.2.
Broadcast Receiver
public class CallReceiver extends BroadcastReceiver {
private MainActivity activity;
public static final String PREFS_PHONE = "Phone";
public static final String PREFS_EMAIL = "Email";
#Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
SharedPreferences settings = context.getSharedPreferences(context.getResources().getString(R.string.app_name),0);
if(settings.contains(PREFS_EMAIL)) {
if (extras != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);
Log.w("MY_DEBUG_TAG", state);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
DatabaseHandler db = new DatabaseHandler(context);
User user;
String phone = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
try {
user = db.getUserByPhone(phone);
Log.d("USER", user.getEmail());
context.startActivity(new Intent(context, FloatingActivity.class)
.putExtra("PHONE", TelephonyManager.EXTRA_INCOMING_NUMBER));
String phoneNumber = extras
.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
settings.edit().putString(PREFS_PHONE, phone).apply();
Log.d("MY_DEBUG_TAG", phoneNumber);
} catch (IndexOutOfBoundsException e) {
Log.i(phone, "Not Found!");
}
}
}
}
}
}
Floating activity
public class FloatingActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chathead_layout);
startService(new Intent(this, FloatingViewService.class)
.putExtra("PHONE", this.getIntent().getStringExtra("PHONE")));
finish();
}
}
Floating Service
public class FloatingViewService extends Service {
private View chatHead;
private View closeChatHead;
private WindowManager windowManager;
private DatabaseHandler db = new DatabaseHandler(this);
private String phone;
private SharedPreferences settings;
private boolean mayRemove;
private Rect chatHeadRect;
private Rect closeHeadRect;
private boolean isHover;
public static final String PREFS_PHONE = "Phone";
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
//Inflate the floating view layout we created
settings = getSharedPreferences(getResources().getString(R.string.app_name),0);
phone = settings.getString(PREFS_PHONE, null);
User user = db.getUserByPhone(phone);
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
chatHead = LayoutInflater.from(this).inflate(R.layout.chathead_layout, null);
closeChatHead = LayoutInflater.from(this).inflate(R.layout.close_chathead_layout, null);
closeChatHead.setVisibility(View.INVISIBLE);
//Add the view to the window.
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
//Specify the view position
params.gravity = Gravity.TOP | Gravity.LEFT; //Initially view will be added to top-left corner
params.x = 0;
params.y = 500;
final WindowManager.LayoutParams params2 = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params2.gravity = Gravity.BOTTOM | Gravity.CENTER;
//Add the view to the window
windowManager.addView(chatHead, params);
windowManager.addView(closeChatHead, params2);
((CircleImageView) chatHead.findViewById(R.id.profile_image)).setImageDrawable(new ColorDrawable(Color.parseColor("#"+user.getColour())));
((TextView) chatHead.findViewById(R.id.pts)).setText(user.getPts()+"");
switch (user.getDirection()) {
case 1:
chatHead.findViewById(R.id.up).setVisibility(View.VISIBLE);
break;
case 0:
chatHead.findViewById(R.id.equal).setVisibility(View.VISIBLE);
break;
case -1:
chatHead.findViewById(R.id.down).setVisibility(View.VISIBLE);
break;
}
final CircleImageView remover = ((CircleImageView) chatHead.findViewById(R.id.remover));
//Set the close button
/*ImageView closeButton = (ImageView) chatHead.findViewById(R.id.close_btn);
closeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//close the service and remove the from from the window
stopSelf();
}
});*/
//Drag and move floating view using user's touch action.
chatHead.findViewById(R.id.chat_head).setOnTouchListener(new View.OnTouchListener() {
private int initialX;
private int initialY;
private float initialTouchX;
private float initialTouchY;
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//remember the initial position.
initialX = params.x;
initialY = params.y;
//get the touch location
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
closeChatHead.setVisibility(View.VISIBLE);
return true;
case MotionEvent.ACTION_MOVE:
//show remover
//Calculate the X and Y coordinates of the view.
params.x = initialX + (int) (event.getRawX() - initialTouchX);
params.y = initialY + (int) (event.getRawY() - initialTouchY);
//Update the layout with new X & Y coordinate
windowManager.updateViewLayout(chatHead, params);
int[] a = new int[2];
int[] b = new int[2];
chatHead.getLocationOnScreen(a);
View remover = closeChatHead.findViewById(R.id.remover);
remover.getLocationOnScreen(b);
chatHeadRect = new Rect(a[0], a[1], a[0] + chatHead.getWidth(), a[1] + chatHead.getHeight());
closeHeadRect = new Rect(b[0], b[1], b[0] + remover.getWidth(), b[1] + remover.getHeight());
if (checkToRemove(Math.round((chatHeadRect.left)) + Math.round((chatHeadRect.width()) / 2),
Math.round((chatHeadRect.top)) + Math.round((chatHeadRect.height()) / 2),
closeHeadRect.left, closeHeadRect.top, closeHeadRect.left + closeHeadRect.width())) {
//Log.d("motion move","intersect = yes");
mayRemove = true;
if (!isHover) {
isHover = !isHover;
windowManager.removeView(closeChatHead);
closeChatHead = LayoutInflater.from(FloatingViewService.this).inflate(R.layout.close_chathead_layout, null);
windowManager.addView(closeChatHead, params2);
}
} else {
//Log.d("motion move","intersect = no");
mayRemove = false;
if (isHover) {
//Log.d("MOTION MOVE2","PASSOU");
isHover = !isHover;
windowManager.removeView(closeChatHead);
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
closeChatHead = inflater.inflate(R.layout.close_chathead_layout, null);
windowManager.addView(closeChatHead, params2);
}
}
closeChatHead.setVisibility(View.VISIBLE);
return true;
case MotionEvent.ACTION_UP:
int Xdiff = (int) (event.getRawX() - initialTouchX);
int Ydiff = (int) (event.getRawY() - initialTouchY);
//The check for Xdiff <10 && YDiff< 10 because sometime elements moves a little while clicking.
//So that is click event.
if (Xdiff < 10 && Ydiff < 10) {
Intent intent = new Intent(FloatingViewService.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
//close the service and remove view from the view hierarchy
stopSelf();
}
if (mayRemove) {
stopSelf();
}
closeChatHead.setVisibility(View.INVISIBLE);
return true;
}
return false;
}
});
}
#Override
public void onDestroy() {
super.onDestroy();
if (chatHead != null) windowManager.removeView(chatHead);
}
private boolean checkToRemove(int x,int y,int left,int top,int left_width){
return (y>(top-50) && x>(left-50) && x<(left_width-50));
}
}
Why not working:
In 7.1 everything is working fine i get the call and the chathead apears.
In 4.4 [LG] I receive the call but nothing happens.
In 4.4 [BQ] Aplication crashes.
Any help?
Found the problem.
Where I'm calling startActivity from outside an activity like:
context.startActivity(new Intent(context, FloatingActivity.class)
.putExtra("PHONE", TelephonyManager.EXTRA_INCOMING_NUMBER));
in android 4.4 it is required to set the flag Intent.FLAG_ACTIVITY_NEW_TASK
o in all ocurrencies i need to call it like:
context.startActivity(new Intent(context, FloatingActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.putExtra("PHONE", TelephonyManager.EXTRA_INCOMING_NUMBER));
I have an Overlay Button in my Android app. I want to show a layout and interact with the views of my Layout when the user click on the button.
For the moment, I show a Toast. How can I do that ?
This is my OverlayShowingService.class :
public class OverlayShowingService extends Service implements OnTouchListener, OnClickListener {
private View topLeftView;
private Button overlayedButton;
private float offsetX;
private float offsetY;
private int originalXPos;
private int originalYPos;
private boolean moving;
private WindowManager wm;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
overlayedButton = new Button(this);
overlayedButton.setText("Overlay button");
overlayedButton.setOnTouchListener(this);
overlayedButton.setBackgroundColor(Color.BLACK);
overlayedButton.setOnClickListener(this);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, PixelFormat.TRANSLUCENT);
params.gravity = Gravity.LEFT | Gravity.TOP;
params.x = 0;
params.y = 0;
wm.addView(overlayedButton, params);
topLeftView = new View(this);
WindowManager.LayoutParams topLeftParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, PixelFormat.TRANSLUCENT);
topLeftParams.gravity = Gravity.LEFT | Gravity.TOP;
topLeftParams.x = 0;
topLeftParams.y = 0;
topLeftParams.width = 0;
topLeftParams.height = 0;
wm.addView(topLeftView, topLeftParams);
}
#Override
public void onDestroy() {
super.onDestroy();
if (overlayedButton != null) {
wm.removeView(overlayedButton);
wm.removeView(topLeftView);
overlayedButton = null;
topLeftView = null;
}
}
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
float x = event.getRawX();
float y = event.getRawY();
moving = false;
int[] location = new int[2];
overlayedButton.getLocationOnScreen(location);
originalXPos = location[0];
originalYPos = location[1];
offsetX = originalXPos - x;
offsetY = originalYPos - y;
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
int[] topLeftLocationOnScreen = new int[2];
topLeftView.getLocationOnScreen(topLeftLocationOnScreen);
System.out.println("topLeftY="+topLeftLocationOnScreen[1]);
System.out.println("originalY="+originalYPos);
float x = event.getRawX();
float y = event.getRawY();
WindowManager.LayoutParams params = (LayoutParams) overlayedButton.getLayoutParams();
int newX = (int) (offsetX + x);
int newY = (int) (offsetY + y);
if (Math.abs(newX - originalXPos) < 1 && Math.abs(newY - originalYPos) < 1 && !moving) {
return false;
}
params.x = newX - (topLeftLocationOnScreen[0]);
params.y = newY - (topLeftLocationOnScreen[1]);
wm.updateViewLayout(overlayedButton, params);
moving = true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
if (moving) {
return true;
}
}
return false;
}
#Override
public void onClick(View v) {
Toast.makeText(this, "Here I want to show a layout with some options in a box", Toast.LENGTH_SHORT).show();
}
}
Send a broadcast when then the button is clicked and start activity from Broadcast receivers onReceive method.
Create a broadcast receiver,Register a Broadcast reciever onCreate() of your service then. Broadcast intent when the button is clicked. Finally the receiver will open a Activity(layout) when the overlay button is clicked.
If you only wanted to open a dialog this is a decent answer. https://stackoverflow.com/a/31221355/4179914
Turns out you can also theme a activity like a dialog as mentioned here
https://stackoverflow.com/a/7918720/4179914
public class OverlayClickReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent toDialog = new Intent(context, Main2Activity.class);
toDialog.setFlags(FLAG_ACTIVITY_NEW_TASK);
context.startActivity(toDialog);
}
}
Send Broadcast on overlay button clicked.
#Override
public void onClick(View v) {
broadcastClick();
}
private void broadcastClick() {
final Intent intent = new Intent("user.clicked.overlay.button");
final LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(this);
broadcastManager.sendBroadcast(intent);
}
Register Broadcast receiver onCreate()
#Override
public void onCreate() {
super.onCreate();
overlayShowing = new OverlayClickReceiver();
.......
}
private void registerClickReceiver() {
LocalBroadcastManager.getInstance(this).registerReceiver(overlayShowing,
new IntentFilter("user.clicked.overlay.button"));
}
Unregister Broadcast receiver onDestroy()
#Override
public void onDestroy() {
super.onDestroy();
..........
unregisterClickReceriver();
}
private void unregisterClickReceriver() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(overlayShowing);
}
From what i understood from the question, you want to get a view form xml files and add it to a window.
You can use View view = LayoutInflater.from(context).inflate(r.layout.overlay,null,false); and add that view with WindowManager.addView(view) to the window.
How to enable zoom for listView for my app?I have found solution here: https://github.com/matrixxun/PullToZoomInListView but I get error: Unexpected cast to PullToZoomListView: layout tag was listView!
listView = (PullToZoomListView)findViewById(R.id.usb_list);
USBProductID.class
public class USBProductID extends AppCompatActivity
{
PullToZoomListView listView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.usbproductid);
//ListView listView = (ListView) findViewById(R.id.usb_list);
listView = (PullToZoomListView)findViewById(R.id.usb_list);
ViewGroup headerView = (ViewGroup)getLayoutInflater().inflate(R.layout.header,listView,false);
listView.addHeaderView(headerView);
String[] items = getResources().getStringArray(R.array.list_items);
ListAdapter adapter = new ListAdapter(this,R.layout.rowlayout,R.id.textcompany,items);
listView.setAdapter(adapter);
}
}
ListAdapter.class
public class ListAdapter extends ArrayAdapter<String>
{
int vg;
String[] item_list;
Context context;
public ListAdapter(Context context, int vg, int id, String[] item_list)
{
super(context, vg, id, item_list);
this.context = context;
this.item_list = item_list;
this.vg = vg;
}
// Hold views of the ListView to improve its scrolling performance
static class ViewHolder
{
public TextView txtmanufacturer;
public TextView txtvendorid;
}
public View getView(int position, View convertView, ViewGroup parent)
{
View rowView = convertView;
if(rowView == null)
{
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(vg,parent,false);
ViewHolder holder = new ViewHolder();
holder.txtmanufacturer = (TextView)rowView.findViewById(R.id.textcompany);
holder.txtvendorid = (TextView)rowView.findViewById(R.id.textvendorid);
rowView.setTag(holder);
}
String[] items = item_list[position].split("_");
ViewHolder holder = (ViewHolder)rowView.getTag();
holder.txtmanufacturer.setText(items[0]);
holder.txtvendorid.setText(items[1]);
return rowView;
}
}
PullToZoomListView.class
public class PullToZoomListView extends ListView implements
AbsListView.OnScrollListener {
private static final int INVALID_VALUE = -1;
private static final String TAG = "PullToZoomListView";
private static final Interpolator sInterpolator = new Interpolator() {
public float getInterpolation(float paramAnonymousFloat) {
float f = paramAnonymousFloat - 1.0F;
return 1.0F + f * (f * (f * (f * f)));
}
};
int mActivePointerId = -1;
private FrameLayout mHeaderContainer;
private int mHeaderHeight;
private ImageView mHeaderImage;
float mLastMotionY = -1.0F;
float mLastScale = -1.0F;
float mMaxScale = -1.0F;
private OnScrollListener mOnScrollListener;
private ScalingRunnalable mScalingRunnalable;
private int mScreenHeight;
private ImageView mShadow;
public PullToZoomListView(Context paramContext) {
super(paramContext);
init(paramContext);
}
public PullToZoomListView(Context paramContext,
AttributeSet paramAttributeSet) {
super(paramContext, paramAttributeSet);
init(paramContext);
}
public PullToZoomListView(Context paramContext,
AttributeSet paramAttributeSet, int paramInt) {
super(paramContext, paramAttributeSet, paramInt);
init(paramContext);
}
private void endScraling() {
if (this.mHeaderContainer.getBottom() >= this.mHeaderHeight)
Log.d("mmm", "endScraling");
this.mScalingRunnalable.startAnimation(200L);
}
private void init(Context paramContext) {
DisplayMetrics localDisplayMetrics = new DisplayMetrics();
((Activity) paramContext).getWindowManager().getDefaultDisplay()
.getMetrics(localDisplayMetrics);
this.mScreenHeight = localDisplayMetrics.heightPixels;
this.mHeaderContainer = new FrameLayout(paramContext);
this.mHeaderImage = new ImageView(paramContext);
int i = localDisplayMetrics.widthPixels;
setHeaderViewSize(i, (int) (9.0F * (i / 16.0F)));
this.mShadow = new ImageView(paramContext);
FrameLayout.LayoutParams localLayoutParams = new FrameLayout.LayoutParams(
-1, -2);
localLayoutParams.gravity = 80;
this.mShadow.setLayoutParams(localLayoutParams);
this.mHeaderContainer.addView(this.mHeaderImage);
this.mHeaderContainer.addView(this.mShadow);
addHeaderView(this.mHeaderContainer);
this.mScalingRunnalable = new ScalingRunnalable();
super.setOnScrollListener(this);
}
private void onSecondaryPointerUp(MotionEvent paramMotionEvent) {
int i = (paramMotionEvent.getAction()) >> 8;
if (paramMotionEvent.getPointerId(i) == this.mActivePointerId)
if (i != 0) {
int j = 1;
this.mLastMotionY = paramMotionEvent.getY(0);
this.mActivePointerId = paramMotionEvent.getPointerId(0);
return;
}
}
private void reset() {
this.mActivePointerId = -1;
this.mLastMotionY = -1.0F;
this.mMaxScale = -1.0F;
this.mLastScale = -1.0F;
}
public ImageView getHeaderView() {
return this.mHeaderImage;
}
public boolean onInterceptTouchEvent(MotionEvent paramMotionEvent) {
return super.onInterceptTouchEvent(paramMotionEvent);
}
protected void onLayout(boolean paramBoolean, int paramInt1, int paramInt2,
int paramInt3, int paramInt4) {
super.onLayout(paramBoolean, paramInt1, paramInt2, paramInt3, paramInt4);
if (this.mHeaderHeight == 0)
this.mHeaderHeight = this.mHeaderContainer.getHeight();
}
#Override
public void onScroll(AbsListView paramAbsListView, int paramInt1,
int paramInt2, int paramInt3) {
Log.d("mmm", "onScroll");
float f = this.mHeaderHeight - this.mHeaderContainer.getBottom();
Log.d("mmm", "f|" + f);
if ((f > 0.0F) && (f < this.mHeaderHeight)) {
Log.d("mmm", "1");
int i = (int) (0.65D * f);
this.mHeaderImage.scrollTo(0, -i);
} else if (this.mHeaderImage.getScrollY() != 0) {
Log.d("mmm", "2");
this.mHeaderImage.scrollTo(0, 0);
}
if (this.mOnScrollListener != null) {
this.mOnScrollListener.onScroll(paramAbsListView, paramInt1,
paramInt2, paramInt3);
}
}
public void onScrollStateChanged(AbsListView paramAbsListView, int paramInt) {
if (this.mOnScrollListener != null)
this.mOnScrollListener.onScrollStateChanged(paramAbsListView,
paramInt);
}
public boolean onTouchEvent(MotionEvent paramMotionEvent) {
Log.d("mmm", "" + (0xFF & paramMotionEvent.getAction()));
switch (0xFF & paramMotionEvent.getAction()) {
case 4:
case 0:
if (!this.mScalingRunnalable.mIsFinished) {
this.mScalingRunnalable.abortAnimation();
}
this.mLastMotionY = paramMotionEvent.getY();
this.mActivePointerId = paramMotionEvent.getPointerId(0);
this.mMaxScale = (this.mScreenHeight / this.mHeaderHeight);
this.mLastScale = (this.mHeaderContainer.getBottom() / this.mHeaderHeight);
break;
case 2:
Log.d("mmm", "mActivePointerId" + mActivePointerId);
int j = paramMotionEvent.findPointerIndex(this.mActivePointerId);
if (j == -1) {
Log.e("PullToZoomListView", "Invalid pointerId="
+ this.mActivePointerId + " in onTouchEvent");
} else {
if (this.mLastMotionY == -1.0F)
this.mLastMotionY = paramMotionEvent.getY(j);
if (this.mHeaderContainer.getBottom() >= this.mHeaderHeight) {
ViewGroup.LayoutParams localLayoutParams = this.mHeaderContainer
.getLayoutParams();
float f = ((paramMotionEvent.getY(j) - this.mLastMotionY + this.mHeaderContainer
.getBottom()) / this.mHeaderHeight - this.mLastScale)
/ 2.0F + this.mLastScale;
if ((this.mLastScale <= 1.0D) && (f < this.mLastScale)) {
localLayoutParams.height = this.mHeaderHeight;
this.mHeaderContainer
.setLayoutParams(localLayoutParams);
return super.onTouchEvent(paramMotionEvent);
}
this.mLastScale = Math.min(Math.max(f, 1.0F),
this.mMaxScale);
localLayoutParams.height = ((int) (this.mHeaderHeight * this.mLastScale));
if (localLayoutParams.height < this.mScreenHeight)
this.mHeaderContainer
.setLayoutParams(localLayoutParams);
this.mLastMotionY = paramMotionEvent.getY(j);
return true;
}
this.mLastMotionY = paramMotionEvent.getY(j);
}
break;
case 1:
reset();
endScraling();
break;
case 3:
int i = paramMotionEvent.getActionIndex();
this.mLastMotionY = paramMotionEvent.getY(i);
this.mActivePointerId = paramMotionEvent.getPointerId(i);
break;
case 5:
onSecondaryPointerUp(paramMotionEvent);
this.mLastMotionY = paramMotionEvent.getY(paramMotionEvent
.findPointerIndex(this.mActivePointerId));
break;
case 6:
}
return super.onTouchEvent(paramMotionEvent);
}
public void setHeaderViewSize(int paramInt1, int paramInt2) {
Object localObject = this.mHeaderContainer.getLayoutParams();
if (localObject == null)
localObject = new LayoutParams(paramInt1, paramInt2);
((ViewGroup.LayoutParams) localObject).width = paramInt1;
((ViewGroup.LayoutParams) localObject).height = paramInt2;
this.mHeaderContainer
.setLayoutParams((ViewGroup.LayoutParams) localObject);
this.mHeaderHeight = paramInt2;
}
public void setOnScrollListener(
OnScrollListener paramOnScrollListener) {
this.mOnScrollListener = paramOnScrollListener;
}
public void setShadow(int paramInt) {
this.mShadow.setBackgroundResource(paramInt);
}
class ScalingRunnalable implements Runnable {
long mDuration;
boolean mIsFinished = true;
float mScale;
long mStartTime;
ScalingRunnalable() {
}
public void abortAnimation() {
this.mIsFinished = true;
}
public boolean isFinished() {
return this.mIsFinished;
}
public void run() {
float f2;
ViewGroup.LayoutParams localLayoutParams;
if ((!this.mIsFinished) && (this.mScale > 1.0D)) {
float f1 = ((float) SystemClock.currentThreadTimeMillis() - (float) this.mStartTime)
/ (float) this.mDuration;
f2 = this.mScale - (this.mScale - 1.0F)
* PullToZoomListView.sInterpolator.getInterpolation(f1);
localLayoutParams = PullToZoomListView.this.mHeaderContainer
.getLayoutParams();
if (f2 > 1.0F) {
Log.d("mmm", "f2>1.0");
localLayoutParams.height = PullToZoomListView.this.mHeaderHeight;
;
localLayoutParams.height = ((int) (f2 * PullToZoomListView.this.mHeaderHeight));
PullToZoomListView.this.mHeaderContainer
.setLayoutParams(localLayoutParams);
PullToZoomListView.this.post(this);
return;
}
this.mIsFinished = true;
}
}
public void startAnimation(long paramLong) {
this.mStartTime = SystemClock.currentThreadTimeMillis();
this.mDuration = paramLong;
this.mScale = ((float) (PullToZoomListView.this.mHeaderContainer
.getBottom()) / PullToZoomListView.this.mHeaderHeight);
this.mIsFinished = false;
PullToZoomListView.this.post(this);
}
}
}
usbproductid.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/usb_list"/>
<TextView
android:text="USB Vendor IDs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:layout_marginTop="18dp"
android:textSize="18sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
Use this :
<PullToZoomListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/usb_list"/>
in you activity
mZoomView.setAdapter(adapter);
mZoomView.getHeaderView().setImageResource(R.drawable.dsf);
mZoomView.getHeaderView().setScaleType(ImageView.ScaleType.CENTER_CROP);
I have opened a window(displayed on top of other activities) on clicking floating view(similar to FB chat head), how to close window when device back button is pressed. Below is my service code
public class ServiceFloating extends Service {
public static int ID_NOTIFICATION = 2018;
private WindowManager windowManager;
private ImageView chatHead;
private PopupWindow pwindo;
boolean mHasDoubleClicked = false;
long lastPressTime;
private Boolean _enable = true;
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onCreate() {
super.onCreate();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
chatHead = new ImageView(this);
chatHead.setImageResource(R.drawable.floating2);
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 100;
windowManager.addView(chatHead, params);
try {
chatHead.setOnTouchListener(new View.OnTouchListener() {
private WindowManager.LayoutParams paramsF = params;
private int initialX;
private int initialY;
private float initialTouchX;
private float initialTouchY;
#Override public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// Get current time in nano seconds.
long pressTime = System.currentTimeMillis();
// If double click...
if (pressTime - lastPressTime <= 300) {
createNotification();
ServiceFloating.this.stopSelf();
mHasDoubleClicked = true;
}
else { // If not double click....
mHasDoubleClicked = false;
}
lastPressTime = pressTime;
initialX = paramsF.x;
initialY = paramsF.y;
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_MOVE:
paramsF.x = initialX + (int) (event.getRawX() - initialTouchX);
paramsF.y = initialY + (int) (event.getRawY() - initialTouchY);
windowManager.updateViewLayout(chatHead, paramsF);
break;
}
return false;
}
});
} catch (Exception e) {
// TODO: handle exception
}
chatHead.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
showOptions(chatHead);
_enable = false;
// Intent intent = new Intent(getApplicationContext(), MainActivity.class);
// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
// getApplicationContext().startActivity(intent);
}
});
}
public void showOptions(View anchor){
windowManager.removeView(anchor);
chatHead = null;
View myView = View.inflate(getApplicationContext(),R.layout.alert_layout,null);
ImageView img1,img2,img3;
img1 = (ImageView) myView.findViewById(R.id.id_img1);
img2 = (ImageView) myView.findViewById(R.id.id_img2);
img3 = (ImageView) myView.findViewById(R.id.id_img3);
img1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"img1 Clicked",Toast.LENGTH_LONG).show();
}
});
img2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
img3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.CENTER;
//params.x = 0;
//params.y = 100;
windowManager.addView(myView, params);
}
#Override
public void onDestroy() {
super.onDestroy();
if (chatHead != null) windowManager.removeView(chatHead);
}
#Override
public void onTaskRemoved(Intent rootIntent) {
// TODO Auto-generated method stub
Intent restartService = new Intent(getApplicationContext(),
this.getClass());
restartService.setPackage(getPackageName());
PendingIntent restartServicePI = PendingIntent.getService(
getApplicationContext(), 1, restartService,
PendingIntent.FLAG_ONE_SHOT);
//Restart the service once it has been killed android
AlarmManager alarmService = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 100, restartServicePI);
}
}
I couldn't add onBackPressed or a onKeyEvent methods inside the service. Please suggest a workaround. TIA
You should handle your view only from activity and use communication components such service binding or broadcasts. You shouldn't create views from service, its not appropriate.
Below is my code that show an overlay with a list view that shows some information. I am updating the data after every 7 sec. I want to remove first list before displaying other.
In may case the ListViews are overlapping.
public class OverlayShowingService extends Service implements OnTouchListener, OnClickListener {
private View topLeftView;
private Button overlayedButton;
ArrayList<String> list = new ArrayList<String>();
/** Declaring an ArrayAdapter to set items to ListView */
ArrayAdapter<String> adapter;
private float offsetX;
private float offsetY;
private int originalXPos;
private int originalYPos;
private boolean moving;
private WindowManager wm;
DispAsync dis;
ListView listView;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),"onStartCommand", Toast.LENGTH_LONG).show();
listView = new ListView(getApplicationContext());
listView.setAlpha(0.0f);
listView.setBackgroundColor(0x55fe4444);
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
public void run() {
try {
dis=new DispAsync();
dis.execute();
} catch (Exception e) {
// TODO Auto-generated catch block
}
}
});
}
};
timer.schedule(doAsynchronousTask, 0, 7000); //execute in every 50000 ms
return super.onStartCommand(intent, flags, startId);
}
public class DispAsync extends AsyncTask<Void, Void, String>
{
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),"onPre", Toast.LENGTH_LONG).show();
//list.clear();
adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, list);
listView.setAdapter(adapter);
super.onPreExecute();
}
#Override
protected String doInBackground(Void... params) {
// TODO Auto-generated method stub
String line = null;
wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
try
{
line = dynamic data;
while (line != null) {
if(line.contains("com"))
{list.add(line);}
line = reader.readLine();
i++;
}
p.waitFor();
}
catch(Exception e){}
return line;
}
#Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),"onPost", Toast.LENGTH_LONG).show();
WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, PixelFormat.TRANSLUCENT);
params.gravity = Gravity.LEFT | Gravity.TOP;
params.x = 0;
params.y = 0;
//adapter.notifyDataSetChanged();
wm.addView(listView, params);
/* topLeftView = new View(getApplicationContext());
WindowManager.LayoutParams topLeftParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, PixelFormat.TRANSLUCENT);
topLeftParams.gravity = Gravity.LEFT | Gravity.TOP;
topLeftParams.x = 0;
topLeftParams.y = 0;
topLeftParams.width = 0;
topLeftParams.height = 0;
wm.addView(topLeftView, topLeftParams);*/
super.onPostExecute(result);
}
}
#Override
public void onDestroy() {
super.onDestroy();
if (overlayedButton != null) {
wm.removeView(overlayedButton);
wm.removeView(topLeftView);
overlayedButton = null;
topLeftView = null;
}
}
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
float x = event.getRawX();
float y = event.getRawY();
moving = false;
int[] location = new int[2];
overlayedButton.getLocationOnScreen(location);
originalXPos = location[0];
originalYPos = location[1];
offsetX = originalXPos - x;
offsetY = originalYPos - y;
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
int[] topLeftLocationOnScreen = new int[2];
topLeftView.getLocationOnScreen(topLeftLocationOnScreen);
System.out.println("topLeftY="+topLeftLocationOnScreen[1]);
System.out.println("originalY="+originalYPos);
float x = event.getRawX();
float y = event.getRawY();
WindowManager.LayoutParams params = (LayoutParams) overlayedButton.getLayoutParams();
int newX = (int) (offsetX + x);
int newY = (int) (offsetY + y);
if (Math.abs(newX - originalXPos) < 1 && Math.abs(newY - originalYPos) < 1 && !moving) {
return false;
}
params.x = newX - (topLeftLocationOnScreen[0]);
params.y = newY - (topLeftLocationOnScreen[1]);
wm.updateViewLayout(overlayedButton, params);
moving = true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
if (moving) {
return true;
}
}
return false;
}
#Override
public void onClick(View v) {
Toast.makeText(this, "Overlay button click event", Toast.LENGTH_SHORT).show();
}
}
logcat
08-11 16:00:55.175: E/AndroidRuntime(11498): FATAL EXCEPTION: main
08-11 16:00:55.175: E/AndroidRuntime(11498): Process: de.mobilej.overlay, PID: 11498
08-11 16:00:55.175: E/AndroidRuntime(11498): java.lang.IllegalStateException: View android.widget.ListView{41db9c50 V.ED.VC. ......I. 0,0-480,422} has already been added to the window manager.
08-11 16:00:55.175: E/AndroidRuntime(11498): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:232)
08-11 16:00:55.175: E/AndroidRuntime(11498): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
08-11 16:00:55.175: E/AndroidRuntime(11498): at de.mobilej.overlay.OverlayShowingService$DispAsync.onPostExecute(OverlayShowingService.java:151)
08-11 16:00:55.175: E/AndroidRuntime(11498): at de.mobilej.overlay.OverlayShowingService$DispAsync.onPostExecute(OverlayShowingService.java:1)
08-11 16:00:55.175: E/AndroidRuntime(11498): at android.os.AsyncTask.finish(AsyncTask.java:632)
08-11 16:00:55.175: E/AndroidRuntime(11498): at android.os.AsyncTask.access$600(AsyncTask.java:177)
08-11 16:00:55.175: E/AndroidRuntime(11498): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
08-11 16:00:55.175: E/AndroidRuntime(11498): at android.os.Handler.dispatchMessage(Handler.java:110)
08-11 16:00:55.175: E/AndroidRuntime(11498): at android.os.Looper.loop(Looper.java:193)
08-11 16:00:55.175: E/AndroidRuntime(11498): at android.app.ActivityThread.main(ActivityThread.java:5388)
08-11 16:00:55.175: E/AndroidRuntime(11498): at java.lang.reflect.Method.invokeNative(Native Method)
08-11 16:00:55.175: E/AndroidRuntime(11498): at java.lang.reflect.Method.invoke(Method.java:515)
Issue is with Listview creation. Currnetly everytime AsynTask is created it is creating the new list view. just create your list view outside of asynctask and global to class so that you can access this listview object within asyncTask like below
private ListView listView;
initialige this listView in your relevant location like below.
listView = new ListView(getApplicationContext());
listView.setAlpha(0.0f);
listView.setBackgroundColor(0x55fe4444);
adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, list);
listView.setAdapter(adapter);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, PixelFormat.TRANSLUCENT);
params.gravity = Gravity.LEFT | Gravity.TOP;
params.x = 0;
params.y = 0;
wm.addView(listView, params);
In your onPostExcute of your asyncTask update your adaper for any list data change like below
adapter.notifyDataSetChanged();
Edit
Complete fix for the reference
public class OverlayShowingService extends Service implements OnTouchListener, OnClickListener {
private View topLeftView;
private Button overlayedButton;
ArrayList<String> list = new ArrayList<String>();
/** Declaring an ArrayAdapter to set items to ListView */
ArrayAdapter<String> adapter;
private float offsetX;
private float offsetY;
private int originalXPos;
private int originalYPos;
private boolean moving;
private WindowManager wm;
DispAsync dis;
ListView listView;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
listView = new ListView(getApplicationContext());
listView.setAlpha(0.0f);
listView.setBackgroundColor(0x55fe4444);
wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, PixelFormat.TRANSLUCENT);
params.gravity = Gravity.LEFT | Gravity.TOP;
params.x = 0;
params.y = 0;
wm.addView(listView, params);
adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, list);
listView.setAdapter(adapter);
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
public void run() {
try {
dis=new DispAsync();
dis.execute();
} catch (Exception e) {
}
}
});
}
};
timer.schedule(doAsynchronousTask, 0, 7000); //execute in every 70000 ms
return super.onStartCommand(intent, flags, startId);
}
public class DispAsync extends AsyncTask<Void, Void, String>
{
#Override
protected void onPreExecute() {
list.clear();
super.onPreExecute();
}
#Override
protected String doInBackground(Void... params) {
String line = null;
try
{
line = dynamic data;
while (line != null) {
if(line.contains("com"))
{list.add(line);}
line = reader.readLine();
i++;
}
p.waitFor();
}
catch(Exception e){}
return line;
}
#Override
protected void onPostExecute(String result) {
adapter.notifyDataSetChanged();
super.onPostExecute(result);
}
}
#Override
public void onDestroy() {
super.onDestroy();
if (overlayedButton != null) {
wm.removeView(overlayedButton);
wm.removeView(topLeftView);
overlayedButton = null;
topLeftView = null;
}
}
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
float x = event.getRawX();
float y = event.getRawY();
moving = false;
int[] location = new int[2];
overlayedButton.getLocationOnScreen(location);
originalXPos = location[0];
originalYPos = location[1];
offsetX = originalXPos - x;
offsetY = originalYPos - y;
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
int[] topLeftLocationOnScreen = new int[2];
topLeftView.getLocationOnScreen(topLeftLocationOnScreen);
System.out.println("topLeftY="+topLeftLocationOnScreen[1]);
System.out.println("originalY="+originalYPos);
float x = event.getRawX();
float y = event.getRawY();
WindowManager.LayoutParams params = (LayoutParams) overlayedButton.getLayoutParams();
int newX = (int) (offsetX + x);
int newY = (int) (offsetY + y);
if (Math.abs(newX - originalXPos) < 1 && Math.abs(newY - originalYPos) < 1 && !moving) {
return false;
}
params.x = newX - (topLeftLocationOnScreen[0]);
params.y = newY - (topLeftLocationOnScreen[1]);
wm.updateViewLayout(overlayedButton, params);
moving = true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
if (moving) {
return true;
}
}
return false;
}
#Override
public void onClick(View v) {
Toast.makeText(this, "Overlay button click event", Toast.LENGTH_SHORT).show();
}
}