summaryrefslogtreecommitdiff
path: root/android/sdremote/src/org/libreoffice/impressremote/fragment/SlidesPagerFragment.java
blob: 46929770987c6a96e633343f0eee4fd8870c011b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/* -*- Mode: Java; 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.fragment;

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.os.Bundle;
import android.os.IBinder;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.TextSwitcher;

import com.actionbarsherlock.app.SherlockFragment;
import org.libreoffice.impressremote.communication.SlideShow;
import org.libreoffice.impressremote.util.Intents;
import org.libreoffice.impressremote.R;
import org.libreoffice.impressremote.adapter.SlidesPagerAdapter;
import org.libreoffice.impressremote.communication.CommunicationService;

public class SlidesPagerFragment extends SherlockFragment implements ServiceConnection, ViewPager.OnPageChangeListener, View.OnClickListener {
    private CommunicationService mCommunicationService;
    private BroadcastReceiver mIntentsReceiver;

    public static SlidesPagerFragment newInstance() {
        return new SlidesPagerFragment();
    }

    @Override
    public View onCreateView(LayoutInflater aInflater, ViewGroup aContainer, Bundle aSavedInstanceState) {
        return aInflater.inflate(R.layout.fragment_slides_pager, aContainer, false);
    }

    @Override
    public void onActivityCreated(Bundle aSavedInstanceState) {
        super.onActivityCreated(aSavedInstanceState);

        bindService();
    }

    private void bindService() {
        Intent aServiceIntent = Intents.buildCommunicationServiceIntent(getActivity());
        getActivity().bindService(aServiceIntent, this, Context.BIND_AUTO_CREATE);
    }

    @Override
    public void onServiceConnected(ComponentName aComponentName, IBinder aBinder) {
        CommunicationService.ServiceBinder aServiceBinder = (CommunicationService.ServiceBinder) aBinder;
        mCommunicationService = aServiceBinder.getService();

        setUpSlidesPager();
    }

    private void setUpSlidesPager() {
        if (!isServiceBound()) {
            return;
        }

        if (!isAdded()) {
            return;
        }

        ViewPager aSlidesPager = getSlidesPager();

        aSlidesPager.setAdapter(buildSlidesAdapter());
        aSlidesPager.setPageMargin(getSlidesMargin());
        aSlidesPager.setOnPageChangeListener(this);

        setUpCurrentSlide();
        setUpCurrentSlideNotes();
    }

    private ViewPager getSlidesPager() {
        return (ViewPager) getView().findViewById(R.id.pager_slides);
    }

    private PagerAdapter buildSlidesAdapter() {
        SlideShow aSlideShow = mCommunicationService.getSlideShow();

        return new SlidesPagerAdapter(getActivity(), aSlideShow, this);
    }

    @Override
    public void onClick(View aView) {
        if (!isLastSlideDisplayed()) {
            showNextTransition();
        }
    }

    private boolean isLastSlideDisplayed() {
        int aCurrentSlideIndex = mCommunicationService.getSlideShow().getHumanCurrentSlideIndex();
        int aSlidesCount = mCommunicationService.getSlideShow().getSlidesCount();

        return aCurrentSlideIndex == aSlidesCount;
    }

    private void showNextTransition() {
        mCommunicationService.getCommandsTransmitter().performNextTransition();
    }

    private int getSlidesMargin() {
        return getResources().getDimensionPixelSize(R.dimen.margin_slide);
    }

    private void setUpCurrentSlide() {
        SlideShow aSlideShow = mCommunicationService.getSlideShow();

        getSlidesPager().setCurrentItem(aSlideShow.getCurrentSlideIndex());
    }

    private void setUpCurrentSlideNotes() {
        SlideShow aSlideShow = mCommunicationService.getSlideShow();

        setUpSlideNotes(aSlideShow.getCurrentSlideIndex());
    }

    @Override
    public void onPageSelected(int aPosition) {
        if (mCommunicationService.getSlideShow().getCurrentSlideIndex() != aPosition) {
            mCommunicationService.getCommandsTransmitter().setCurrentSlide(aPosition);
        }

        setUpSlideNotes(aPosition);
    }

    private void setUpSlideNotes(int aSlideIndex) {
        if (!isSlideNotesLayoutAvailable()) {
            return;
        }

        if (!isSlideVisible(aSlideIndex)) {
            return;
        }

        if (!areSlideNotesAvailable(aSlideIndex)) {
            hideSlideNotes();
            return;
        }

        showSlideNotes(aSlideIndex);
        scrollSlideNotes();
    }

    private boolean isSlideNotesLayoutAvailable() {
        ViewGroup aSlideNotesLayout = (ViewGroup) getView().findViewById(R.id.layout_notes);

        return aSlideNotesLayout != null;
    }

    private boolean isSlideVisible(int aSlideIndex) {
        return aSlideIndex == getSlidesPager().getCurrentItem();
    }

    private boolean areSlideNotesAvailable(int aSlideIndex) {
        String aSlideNotes = mCommunicationService.getSlideShow().getSlideNotes(aSlideIndex);

        return !TextUtils.isEmpty(Html.fromHtml(aSlideNotes).toString().trim());
    }

    private void showSlideNotes(int aSlideIndex) {
        TextSwitcher aSlideNotesSwitcher = getSlideNotesSwitcher();
        String aSlideNotes = mCommunicationService.getSlideShow().getSlideNotes(aSlideIndex);

        aSlideNotesSwitcher.setText(Html.fromHtml(aSlideNotes));
    }

    private TextSwitcher getSlideNotesSwitcher() {
        return (TextSwitcher) getView().findViewById(R.id.text_switcher_notes);
    }

    private void scrollSlideNotes() {
        ScrollView aSlideNotesScroll = (ScrollView) getView().findViewById(R.id.scroll_notes);

        aSlideNotesScroll.scrollTo(0, 0);
    }

    private void hideSlideNotes() {
        TextSwitcher aSlideNotesSwitcher = getSlideNotesSwitcher();

        aSlideNotesSwitcher.setText(getString(R.string.message_notes_empty));
    }

    @Override
    public void onPageScrolled(int aPosition, float aPositionOffset, int aPositionOffsetPixels) {
    }

    @Override
    public void onPageScrollStateChanged(int aState) {
    }

    @Override
    public void onServiceDisconnected(ComponentName aComponentName) {
        mCommunicationService = null;
    }

    @Override
    public void onResume() {
        super.onResume();

        registerIntentsReceiver();
    }

    private void registerIntentsReceiver() {
        mIntentsReceiver = new IntentsReceiver(this);
        IntentFilter aIntentFilter = buildIntentsReceiverFilter();

        getBroadcastManager().registerReceiver(mIntentsReceiver, aIntentFilter);
    }

    private static final class IntentsReceiver extends BroadcastReceiver {
        private final SlidesPagerFragment mSlidesPagerFragment;

        private IntentsReceiver(SlidesPagerFragment aSlidesGridFragment) {
            mSlidesPagerFragment = aSlidesGridFragment;
        }

        @Override
        public void onReceive(Context aContext, Intent aIntent) {
            if (Intents.Actions.SLIDE_SHOW_RUNNING.equals(aIntent.getAction())) {
                mSlidesPagerFragment.setUpSlidesPager();
                return;
            }

            if (Intents.Actions.SLIDE_SHOW_STOPPED.equals(aIntent.getAction())) {
                mSlidesPagerFragment.setUpSlidesPager();
                return;
            }

            if (Intents.Actions.SLIDE_CHANGED.equals(aIntent.getAction())) {
                mSlidesPagerFragment.setUpCurrentSlide();
                return;
            }

            if (Intents.Actions.SLIDE_PREVIEW.equals(aIntent.getAction())) {
                int aSlideIndex = aIntent.getIntExtra(Intents.Extras.SLIDE_INDEX, 0);

                mSlidesPagerFragment.refreshSlide(aSlideIndex);
                return;
            }

            if (Intents.Actions.SLIDE_NOTES.equals(aIntent.getAction())) {
                int aSlideIndex = aIntent.getIntExtra(Intents.Extras.SLIDE_INDEX, 0);

                mSlidesPagerFragment.setUpSlideNotes(aSlideIndex);
                return;
            }
        }
    }

    private IntentFilter buildIntentsReceiverFilter() {
        IntentFilter aIntentFilter = new IntentFilter();
        aIntentFilter.addAction(Intents.Actions.SLIDE_SHOW_RUNNING);
        aIntentFilter.addAction(Intents.Actions.SLIDE_SHOW_STOPPED);
        aIntentFilter.addAction(Intents.Actions.SLIDE_CHANGED);
        aIntentFilter.addAction(Intents.Actions.SLIDE_PREVIEW);
        aIntentFilter.addAction(Intents.Actions.SLIDE_NOTES);

        return aIntentFilter;
    }

    private LocalBroadcastManager getBroadcastManager() {
        Context aContext = getActivity().getApplicationContext();

        return LocalBroadcastManager.getInstance(aContext);
    }

    private void refreshSlide(int aSlideIndex) {
        // Refresh only loaded slides to avoid images blinking on large slides count.
        // There is no way to invalidate only a certain slide.

        int aCurrentSlideIndex = mCommunicationService.getSlideShow().getCurrentSlideIndex();

        if (aSlideIndex == aCurrentSlideIndex) {
            refreshSlidesPager();
            return;
        }

        int aSlidesOffscreenCount = getSlidesPager().getOffscreenPageLimit();

        if (aSlideIndex < aCurrentSlideIndex - aSlidesOffscreenCount) {
            return;
        }

        if (aSlideIndex > aCurrentSlideIndex + aSlidesOffscreenCount) {
            return;
        }

        refreshSlidesPager();
    }

    private void refreshSlidesPager() {
        getSlidesPager().getAdapter().notifyDataSetChanged();
    }

    @Override
    public void onPause() {
        super.onPause();

        unregisterIntentsReceiver();
    }

    private void unregisterIntentsReceiver() {
        try {
            getBroadcastManager().unregisterReceiver(mIntentsReceiver);
        } catch (IllegalArgumentException e) {
            // Receiver not registered.
            // Fixed in Honeycomb: Android’s issue #6191.
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        unbindService();
    }

    private void unbindService() {
        if (!isServiceBound()) {
            return;
        }

        getActivity().unbindService(this);
    }

    private boolean isServiceBound() {
        return mCommunicationService != null;
    }
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */