Fix losing communication service on screen rotation.

Change-Id: I8ae72857ce65783fe79cd3b911b83b4c70deece6
This commit is contained in:
Andrzej J.R. Hunt
2012-08-21 14:35:39 +02:00
parent 15d272dfcc
commit fa3a32a8da
5 changed files with 350 additions and 529 deletions

View File

@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/org.libreoffice.impressremote"
android:id="@+id/presentation_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
@@ -15,9 +16,10 @@
android:id="@+id/presentation_coverflow"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="5dip"
coverflow:imageHeight="150dip"
coverflow:imageWidth="180dip"
android:layout_margin="5dp"
android:layout_marginLeft="10dp"
coverflow:imageHeight="200dip"
coverflow:imageWidth="240dip"
coverflow:withReflection="false" />
<TextView
@@ -36,6 +38,8 @@
android:layout_marginRight="6dp"
android:contentDescription="@string/presentation_ui_resizehandle"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="10dp"
android:paddingTop="5dp"
android:scaleType="fitXY"
android:src="@drawable/handle" />

View File

@@ -112,8 +112,6 @@ public class PresentationActivity extends SherlockFragmentActivity {
mCommunicationService = ((CommunicationService.CBinder) aService)
.getService();
mPresentationFragment
.setCommunicationService(mCommunicationService);
mThumbnailFragment.setCommunicationService(mCommunicationService);
}

View File

@@ -6,9 +6,11 @@ import org.libreoffice.impressremote.communication.SlideShow;
import pl.polidea.coverflow.AbstractCoverFlowImageAdapter;
import pl.polidea.coverflow.CoverFlow;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Canvas;
@@ -16,6 +18,7 @@ import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.content.LocalBroadcastManager;
import android.view.LayoutInflater;
import android.view.MotionEvent;
@@ -39,7 +42,6 @@ public class PresentationFragment extends SherlockFragment {
private TextView mNumberText;
private CommunicationService mCommunicationService;
private SlideShow mSlideShow;
private float mOriginalCoverflowWidth;
private float mOriginalCoverflowHeight;
@@ -47,8 +49,37 @@ public class PresentationFragment extends SherlockFragment {
private float mNewCoverflowWidth = 0;
private float mNewCoverflowHeight = 0;
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName aClassName,
IBinder aService) {
mCommunicationService = ((CommunicationService.CBinder) aService)
.getService();
if (mTopView != null) {
mTopView.setAdapter(new ThumbnailAdapter(mContext,
mCommunicationService.getSlideShow()));
mTopView.setSelection(mCommunicationService.getSlideShow()
.getCurrentSlide(), true);
mTopView.setOnItemSelectedListener(new ClickListener());
}
updateSlideNumberDisplay();
}
@Override
public void onServiceDisconnected(ComponentName aClassName) {
mCommunicationService = null;
}
};
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
getActivity().bindService(
new Intent(getActivity().getApplicationContext(),
CommunicationService.class),
mConnection, Context.BIND_IMPORTANT);
mContext = getActivity().getApplicationContext();
container.removeAllViews();
View v = inflater.inflate(R.layout.fragment_presentation, container,
@@ -69,9 +100,6 @@ public class PresentationFragment extends SherlockFragment {
mHandle = (ImageView) v.findViewById(R.id.presentation_handle);
mHandle.setOnTouchListener(new SizeListener());
// Call again to set things up if necessary.
setCommunicationService(mCommunicationService);
// Save the height/width for future reference
mOriginalCoverflowHeight = mTopView.getImageHeight();
mOriginalCoverflowWidth = mTopView.getImageWidth();
@@ -95,11 +123,13 @@ public class PresentationFragment extends SherlockFragment {
LocalBroadcastManager
.getInstance(getActivity().getApplicationContext())
.registerReceiver(mListener, aFilter);
return v;
}
@Override
public void onDestroyView() {
getActivity().unbindService(mConnection);
super.onDestroyView();
LocalBroadcastManager
.getInstance(getActivity().getApplicationContext())
@@ -108,9 +138,11 @@ public class PresentationFragment extends SherlockFragment {
}
private void updateSlideNumberDisplay() {
int aSlide = mSlideShow.getCurrentSlide();
mNumberText.setText((aSlide + 1) + "/" + mSlideShow.getSize());
mNotes.loadData(mSlideShow.getNotes(aSlide), "text/html", null);
int aSlide = mCommunicationService.getSlideShow().getCurrentSlide();
mNumberText.setText((aSlide + 1) + "/"
+ mCommunicationService.getSlideShow().getSize());
mNotes.loadData(mCommunicationService.getSlideShow().getNotes(aSlide),
"text/html", null);
}
// -------------------------------------------------- RESIZING LISTENER ----
@@ -207,22 +239,6 @@ public class PresentationFragment extends SherlockFragment {
}
// ---------------------------------------------------- MESSAGE HANDLER ----
public void setCommunicationService(
CommunicationService aCommunicationService) {
mCommunicationService = aCommunicationService;
if (mCommunicationService == null)
return;
mSlideShow = mCommunicationService.getSlideShow();
if (mTopView != null && mSlideShow != null) {
mTopView.setAdapter(new ThumbnailAdapter(mContext, mSlideShow));
mTopView.setSelection(mSlideShow.getCurrentSlide(), true);
mTopView.setOnItemSelectedListener(new ClickListener());
}
updateSlideNumberDisplay();
}
private BroadcastReceiver mListener = new BroadcastReceiver() {
@Override

View File

@@ -1,197 +0,0 @@
///* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
///*
// * This file is part of the LibreOffice project.
// *
// * This Source Code Form is subject to the terms of the Mozilla Public
// * License, v. 2.0. If a copy of the MPL was not distributed with this
// * file, You can obtain one at http://mozilla.org/MPL/2.0/.
// */
//package org.libreoffice.impressremote.communication;
//
//import org.libreoffice.impressremote.PresentationActivity;
//import org.libreoffice.impressremote.R;
//import org.libreoffice.impressremote.communication.Server.Protocol;
//
//import android.app.Activity;
//import android.content.ComponentName;
//import android.content.Context;
//import android.content.Intent;
//import android.content.ServiceConnection;
//import android.graphics.Bitmap;
//import android.os.Bundle;
//import android.os.Handler;
//import android.os.IBinder;
//import android.os.Message;
//import android.os.Messenger;
//import android.view.View;
//import android.view.View.OnClickListener;
//import android.widget.Button;
//import android.widget.ImageView;
//import android.widget.TextView;
//
//public class TestClient extends Activity {
//
// private boolean mCurrentPreviewImageMissing = false;
//
// private boolean mIsBound = false;
//
// private CommunicationService mCommunicationService;
//
// final Messenger mMessenger = new Messenger(new MessageHandler());
//
// /** Called when the activity is first created. */
// @Override
// public void onCreate(Bundle savedInstanceState) {
// super.onCreate(savedInstanceState);
// setContentView(R.layout.testlayout);
// setupUI();
//
// }
//
// @Override
// protected void onResume() {
// super.onResume();
// doBindService();
//
// }
//
// // FIXME: move all necessary code to CommunicationService.onUnbind
//
// @Override
// protected void onPause() {
// super.onPause();
// }
//
// @Override
// public void onBackPressed() {
// // TODO Auto-generated method stub
// mCommunicationService.disconnect();
// stopService(new Intent(this, CommunicationService.class));
// doUnbindService();
// finish();
// super.onBackPressed();
// }
//
// @Override
// protected void onStop() {
// // TODO Auto-generated method stub
// super.onStop();
// // mCommunicationService.disconnect();
// // stopService(new Intent(this, CommunicationService.class));
// }
//
// private ServiceConnection mConnection = new ServiceConnection() {
// @Override
// public void onServiceConnected(ComponentName aClassName,
// IBinder aService) {
// mCommunicationService = ((CommunicationService.CBinder) aService)
// .getService();
// mCommunicationService.connectTo(new Server(Protocol.NETWORK,
// "10.0.2.2", "TestServer", 0l));
// mCommunicationService.setActivityMessenger(mMessenger);
// enableButtons(true);
// }
//
// @Override
// public void onServiceDisconnected(ComponentName aClassName) {
// mCommunicationService = null;
// enableButtons(false);
// }
// };
//
// void doBindService() {
// Intent aIntent = new Intent(this, CommunicationService.class);
// startService(aIntent);
// bindService(aIntent, mConnection, Context.BIND_IMPORTANT);
// mIsBound = true;
// }
//
// void doUnbindService() {
// mCommunicationService.setActivityMessenger(null);
// if (mIsBound) {
// unbindService(mConnection);
// mIsBound = false;
// }
// }
//
// private Button mButtonNext;
//
// private Button mButtonPrevious;
//
// private ImageView mImageView;
//
// private TextView mSlideLabel;
//
// private void setupUI() {
// mButtonNext = (Button) findViewById(R.id.button_next);
// mButtonPrevious = (Button) findViewById(R.id.button_previous);
// mImageView = (ImageView) findViewById(R.id.image_preview);
// mSlideLabel = (TextView) findViewById(R.id.label_curSlide);
//
// enableButtons(false);
//
// mButtonNext.setOnClickListener(new OnClickListener() {
//
// @Override
// public void onClick(View v) {
// mCommunicationService.getTransmitter().nextTransition();
//
// }
//
// });
//
// mButtonPrevious.setOnClickListener(new OnClickListener() {
//
// @Override
// public void onClick(View v) {
// mCommunicationService.getTransmitter().previousTransition();
//
// }
//
// });
//
// Button mThumbnailButton = (Button) findViewById(R.id.button_thumbnail);
//
// mThumbnailButton.setOnClickListener(new OnClickListener() {
// @Override
// public void onClick(View v) {
// Intent aIntent = new Intent(TestClient.this,
// PresentationActivity.class);
// startActivity(aIntent);
// }
// });
//
// }
//
// private void enableButtons(boolean aEnabled) {
// mButtonNext.setEnabled(aEnabled);
// mButtonPrevious.setEnabled(aEnabled);
// }
//
// class MessageHandler extends Handler {
// @Override
// public void handleMessage(Message aMessage) {
// Bundle aData = aMessage.getData();
// switch (aMessage.what) {
// case CommunicationService.MSG_SLIDE_CHANGED:
// int newSlide = aData.getInt("slide_number");
// mSlideLabel.setText("Slide " + newSlide);
// mCurrentPreviewImageMissing = true;
// // We continue on to try and update the image.
// case CommunicationService.MSG_SLIDE_PREVIEW:
// int aSlideNumber = aData.getInt("slide_number");
// if (mCurrentPreviewImageMissing) {
// Bitmap aImage = mCommunicationService.getSlideShow()
// .getImage(aSlideNumber);
// if (aImage != null) {
// mImageView.setImageBitmap(aImage);
// mCurrentPreviewImageMissing = false;
// }
// }
// break;
//
// }
// }
// }
//}
///* vim:set shiftwidth=4 softtabstop=4 expandtab: */

View File

@@ -47,12 +47,12 @@ public class CoverFlow extends Gallery {
/**
* The maximum angle the Child ImageView will be rotated by.
*/
private int mMaxRotationAngle = 60;
private int mMaxRotationAngle = 30;
/**
* The maximum zoom on the centre Child.
*/
private int mMaxZoom = -160;
private int mMaxZoom = -100;
/**
* The Centre of the Coverflow.