Advertisement
Guest User

Untitled

a guest
Mar 6th, 2015
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.40 KB | None | 0 0
  1. //
  2. private static final int TRAY_HIDDEN_FRACTION = 6; // Controls fraction of the tray hidden when open
  3. private static final int TRAY_MOVEMENT_REGION_FRACTION = 6; // Controls fraction of y-axis on screen within which the tray stays.
  4. private static final int TRAY_CROP_FRACTION = 12; // Controls fraction of the tray chipped at the right end.
  5. private static final int ANIMATION_FRAME_RATE = 30; // Animation frame rate per second.
  6. private static final int TRAY_DIM_X_DP = 400; // Width of the tray in dps
  7. private static final int TRAY_DIM_Y_DP = 400; // Height of the tray in dps
  8. private static final int BUTTONS_DIM_Y_DP = 27; // Height of the buttons in dps
  9.  
  10. // Layout containers for various widgets
  11. private WindowManager mWindowManager; // Reference to the window
  12. private WindowManager.LayoutParams mRootLayoutParams; // Parameters of the root layout
  13. private RelativeLayout mRootLayout; // Root layout
  14. private RelativeLayout mContentContainerLayout;// Contains everything other than buttons and song info
  15. //private RelativeLayout mLogoLayout; // Contains Cpotify logo
  16. private FrameLayout mAlbumCoverLayout; // Contains album cover of the active song
  17. private int mStartDragX;
  18. //private int mStartDragY; // Unused as yet
  19. private int mPrevDragX;
  20. private int mPrevDragY;
  21. public Call call;
  22. private boolean mIsTrayOpen = true;
  23.  
  24. // Controls for animations
  25. private Timer mTrayAnimationTimer;
  26. private TrayAnimationTimerTask mTrayTimerTask;
  27. private Handler mAnimationHandler = new Handler();
  28. private VideoInFrame videoInView;
  29. Handler handler;
  30.  
  31. // Mock song data
  32.  
  33. @Override
  34. public IBinder onBind(Intent intent) {
  35. // Not used
  36. return null;
  37. }
  38.  
  39. @Override
  40. public void onCreate() {
  41. handler = new Handler();
  42. // Get references to all the views and add them to root view as needed.
  43. mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
  44.  
  45. mRootLayout = (RelativeLayout) LayoutInflater.from(this).
  46. inflate(R.layout.floating_video_call, null);
  47. mContentContainerLayout = (RelativeLayout) mRootLayout.findViewById(R.id.content_container);
  48. mContentContainerLayout.setOnTouchListener(new TrayTouchListener());
  49.  
  50. mAlbumCoverLayout = (FrameLayout) mRootLayout.findViewById(R.id.video_frame);
  51. videoInView = (VideoInFrame) mRootLayout.findViewById(R.id.video_in_master);
  52.  
  53. mRootLayoutParams = new WindowManager.LayoutParams(
  54. WindowManager.LayoutParams.MATCH_PARENT,
  55. WindowManager.LayoutParams.MATCH_PARENT,
  56. WindowManager.LayoutParams.TYPE_PHONE,
  57. WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
  58. | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
  59. PixelFormat.TRANSLUCENT);
  60.  
  61. mRootLayoutParams.gravity = Gravity.TOP | Gravity.LEFT;
  62. mWindowManager.addView(mRootLayout, mRootLayoutParams);
  63.  
  64.  
  65. // Post these actions at the end of looper message queue so that the layout is
  66. // fully inflated once these functions execute
  67. mRootLayout.postDelayed(new Runnable() {
  68.  
  69.  
  70. @Override
  71. public void run() {
  72.  
  73. // Reusable variables
  74. RelativeLayout.LayoutParams params;
  75. mRootLayoutParams.x = -mAlbumCoverLayout.getLayoutParams().width;
  76. mRootLayoutParams.y = (getApplicationContext().getResources().getDisplayMetrics().heightPixels-mRootLayout.getHeight()) / 2;
  77. mWindowManager.updateViewLayout(mRootLayout, mRootLayoutParams);
  78.  
  79. mRootLayout.setVisibility(View.VISIBLE);
  80.  
  81. call = Rtcc.instance() == null ? null : Rtcc.instance().getCall(DataHandler.getIntPreferences(AppConstant.SP_CALLID));
  82. if(call != null){
  83. showVideoCallView(call);
  84.  
  85. }
  86.  
  87.  
  88. // Animate the Tray
  89. mTrayTimerTask = new TrayAnimationTimerTask();
  90. mTrayAnimationTimer = new Timer();
  91. mTrayAnimationTimer.schedule(mTrayTimerTask, 0, ANIMATION_FRAME_RATE);
  92. }
  93. }, ANIMATION_FRAME_RATE);
  94.  
  95.  
  96. }
  97.  
  98. // The phone orientation has changed. Update the widget's position.
  99. @Override
  100. public void onConfigurationChanged(Configuration newConfig) {
  101. super.onConfigurationChanged(newConfig);
  102. if (mIsTrayOpen)
  103. mRootLayoutParams.x = -mRootLayout.getWidth()/TRAY_HIDDEN_FRACTION;
  104. else
  105. mRootLayoutParams.x = -mAlbumCoverLayout.getWidth();
  106. mRootLayoutParams.y = (getResources().getDisplayMetrics().heightPixels-mRootLayout.getHeight()) / 2;
  107. mWindowManager.updateViewLayout(mRootLayout, mRootLayoutParams);
  108. animateButtons();
  109. }
  110. @RtccEventListener
  111. public void onCallSendingVideoChangedEvent(VideoOutEvent event) {
  112. if (!event.isMatching(this.call)) {
  113. return;
  114. }
  115.  
  116. //callControls.updateCallControls(call, true,false);
  117. }
  118.  
  119. private void showVideoCallView(Call call) {
  120.  
  121. if (this.call == null || this.call.getStatus() == Call.CallStatus.ENDED) {
  122. return;
  123. }
  124. Contact contact = this.call.getContact(Contact.DEFAULT_CONTACT_ID);
  125. if (contact != null) {
  126. // findViewById(R.id.connectedMsgView).setVisibility(View.GONE);
  127. mRootLayout.findViewById(R.id.video_frame).setVisibility(View.VISIBLE);
  128. this.videoInView.setVisibility(View.VISIBLE);
  129. this.videoInView.setBackgroundResource(R.color.green_blue_1);
  130. contact.setView(this.videoInView);
  131. // call.setVideoOut(videoOutView);
  132. Call.VideoProfile videoProfile = call.getVideoInProfile(Contact.DEFAULT_CONTACT_ID);
  133. call.setInVideoProfile(videoProfile.SD);
  134. //call.setInVideoProfile(Call.VideoProfile.SD);
  135. // callControls.setOnCallControlsListener(this);
  136. // callControls.updateCallControls(call, true,false);
  137. // isCallInProgress = true;
  138. }
  139. }
  140.  
  141.  
  142. @Override
  143. public int onStartCommand(Intent intent, int flags, int startId) {
  144.  
  145.  
  146. if (intent.getBooleanExtra("stop_service", false)){
  147. // If it's a call from the notification, stop the service.
  148. stopSelf();
  149. }else{
  150. // Make the service run in foreground so that the system does not shut it down.
  151. Intent notificationIntent = new Intent(this, FloatingVideoCallService.class);
  152. notificationIntent.putExtra("stop_service", true);
  153. PendingIntent pendingIntent = PendingIntent.getService(this, 0, notificationIntent, 0);
  154. Notification notification = new Notification(
  155. R.drawable.ic_launcher,
  156. "Spotify tray launched",
  157. System.currentTimeMillis());
  158. notification.setLatestEventInfo(
  159. this,
  160. "Spotify tray",
  161. "Tap to close the widget.",
  162. pendingIntent);
  163. startForeground(86, notification);
  164. }
  165. return START_STICKY;
  166. }
  167.  
  168. // The app is closing.
  169. @Override
  170. public void onDestroy() {
  171. //mPlaylist.stopCurrentSong();
  172. if (mRootLayout != null)
  173. mWindowManager.removeView(mRootLayout);
  174. }
  175.  
  176. // Drags the tray as per touch info
  177. private void dragTray(int action, int x, int y){
  178. switch (action){
  179. case MotionEvent.ACTION_DOWN:
  180.  
  181. // Cancel any currently running animations/automatic tray movements.
  182. if (mTrayTimerTask!=null){
  183. mTrayTimerTask.cancel();
  184. mTrayAnimationTimer.cancel();
  185. }
  186.  
  187. // Store the start points
  188. mStartDragX = x;
  189. //mStartDragY = y;
  190. mPrevDragX = x;
  191. mPrevDragY = y;
  192. break;
  193.  
  194. case MotionEvent.ACTION_MOVE:
  195.  
  196. // Calculate position of the whole tray according to the drag, and update layout.
  197. float deltaX = x-mPrevDragX;
  198. float deltaY = y-mPrevDragY;
  199. mRootLayoutParams.x += deltaX;
  200. mRootLayoutParams.y += deltaY;
  201. mPrevDragX = x;
  202. mPrevDragY = y;
  203. animateButtons();
  204. mWindowManager.updateViewLayout(mRootLayout, mRootLayoutParams);
  205. break;
  206.  
  207. case MotionEvent.ACTION_UP:
  208. case MotionEvent.ACTION_CANCEL:
  209.  
  210. // When the tray is released, bring it back to "open" or "closed" state.
  211. if ((mIsTrayOpen && (x-mStartDragX)<=0) ||
  212. (!mIsTrayOpen && (x-mStartDragX)>=0))
  213. mIsTrayOpen = !mIsTrayOpen;
  214.  
  215. mTrayTimerTask = new TrayAnimationTimerTask();
  216. mTrayAnimationTimer = new Timer();
  217. mTrayAnimationTimer.schedule(mTrayTimerTask, 0, ANIMATION_FRAME_RATE);
  218. break;
  219. }
  220. }
  221.  
  222. // Listens to the touch events on the tray.
  223. private class TrayTouchListener implements OnTouchListener {
  224. @Override
  225. public boolean onTouch(View v, MotionEvent event) {
  226.  
  227. final int action = event.getActionMasked();
  228.  
  229. switch (action) {
  230. case MotionEvent.ACTION_DOWN:
  231. case MotionEvent.ACTION_MOVE:
  232. case MotionEvent.ACTION_UP:
  233. case MotionEvent.ACTION_CANCEL:
  234. // Filter and redirect the events to dragTray()
  235. dragTray(action, (int)event.getRawX(), (int)event.getRawY());
  236. break;
  237. default:
  238. return false;
  239. }
  240. return true;
  241.  
  242. }
  243. }
  244.  
  245. // Timer for animation/automatic movement of the tray.
  246. private class TrayAnimationTimerTask extends TimerTask{
  247.  
  248. // Ultimate destination coordinates toward which the tray will move
  249. int mDestX;
  250. int mDestY;
  251.  
  252. public TrayAnimationTimerTask(){
  253.  
  254. // Setup destination coordinates based on the tray state.
  255. super();
  256. if (!mIsTrayOpen){
  257. mDestX = -mAlbumCoverLayout.getWidth();
  258. }else{
  259. mDestX = -mRootLayout.getWidth()/TRAY_HIDDEN_FRACTION;
  260. }
  261.  
  262. // Keep upper edge of the widget within the upper limit of screen
  263. int screenHeight = getResources().getDisplayMetrics().heightPixels;
  264. mDestY = Math.max(
  265. screenHeight/TRAY_MOVEMENT_REGION_FRACTION,
  266. mRootLayoutParams.y);
  267.  
  268. // Keep lower edge of the widget within the lower limit of screen
  269. mDestY = Math.min(
  270. ((TRAY_MOVEMENT_REGION_FRACTION-1)*screenHeight)/TRAY_MOVEMENT_REGION_FRACTION - mRootLayout.getWidth(),
  271. mDestY);
  272. }
  273.  
  274. // This function is called after every frame.
  275. @Override
  276. public void run() {
  277.  
  278. // handler is used to run the function on main UI thread in order to
  279. // access the layouts and UI elements.
  280. mAnimationHandler.post(new Runnable() {
  281. @Override
  282. public void run() {
  283.  
  284. // Update coordinates of the tray
  285. mRootLayoutParams.x = (2*(mRootLayoutParams.x-mDestX))/3 + mDestX;
  286. mRootLayoutParams.y = (2*(mRootLayoutParams.y-mDestY))/3 + mDestY;
  287. mWindowManager.updateViewLayout(mRootLayout, mRootLayoutParams);
  288. animateButtons();
  289.  
  290. // Cancel animation when the destination is reached
  291. if (Math.abs(mRootLayoutParams.x-mDestX)<2 && Math.abs(mRootLayoutParams.y-mDestY)<2){
  292. TrayAnimationTimerTask.this.cancel();
  293. mTrayAnimationTimer.cancel();
  294. }
  295. }
  296. });
  297. }
  298. }
  299.  
  300. // This function animates the buttons based on the position of the tray.
  301. private void animateButtons(){
  302.  
  303. // Animate only if the tray is between open and close state.
  304. if (mRootLayoutParams.x < -mRootLayout.getWidth()/TRAY_HIDDEN_FRACTION){
  305.  
  306. // Scale the distance between open and close states to 0-1.
  307. float relativeDistance = (mRootLayoutParams.x + mAlbumCoverLayout.getWidth())/(float)
  308. (-mRootLayout.getWidth()/TRAY_HIDDEN_FRACTION + mAlbumCoverLayout.getWidth());
  309.  
  310. // Limit it to 0-1 if it goes beyond 0-1 for any reason.
  311. relativeDistance=Math.max(relativeDistance, 0);
  312. relativeDistance=Math.min(relativeDistance, 1);
  313.  
  314. // Setup animations
  315. AnimationSet animations = new AnimationSet(true);
  316. animations.setFillAfter(true);
  317. Animation animationAlpha = new AlphaAnimation(
  318. relativeDistance,
  319. relativeDistance);
  320. animations.addAnimation(animationAlpha);
  321.  
  322. Animation animationScale = new ScaleAnimation(
  323. relativeDistance,
  324. relativeDistance,
  325. relativeDistance,
  326. relativeDistance);
  327. animations.addAnimation(animationScale);
  328.  
  329. // Play the animations
  330. mAlbumCoverLayout.startAnimation(animationAlpha);
  331. }else{
  332.  
  333. // Clear all animations if the tray is being dragged - that is, when it is beyond the
  334. // normal open state.
  335. mAlbumCoverLayout.clearAnimation();
  336. }
  337. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement