Project

General

Profile

Download (4.64 KB) Statistics
| Branch: | Revision:

library / src / main / java / org / distorted / library / EffectMessageSender.java @ a09ada4c

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted is free software: you can redistribute it and/or modify                             //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Distorted is distributed in the hope that it will be useful,                                  //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.library;
21

    
22
import org.distorted.library.message.EffectListener;
23
import org.distorted.library.message.EffectMessage;
24

    
25
import java.util.Vector;
26

    
27
///////////////////////////////////////////////////////////////////////////////////////////////////
28

    
29
final class EffectMessageSender extends Thread
30
  {
31
  private class Message
32
    {
33
    EffectListener mListener;
34
    EffectMessage mMessage;
35
    long mEffectID;
36
    int mEffectName;
37
    long mBitmapID;
38

    
39
    Message(EffectListener l, EffectMessage m, long id, int name, long bmpID)
40
      {
41
      mListener   = l;
42
      mMessage    = m;
43
      mEffectID   = id;
44
      mEffectName = name;
45
      mBitmapID   = bmpID;
46
      }
47
    }
48
  
49
  private static Vector<Message> mList =null;
50
  private static EffectMessageSender mThis=null;
51
  private static volatile boolean mNotify  = false;
52
  private static volatile boolean mRunning = false;
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55
   
56
  private EffectMessageSender() 
57
    {
58
    }
59

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

    
62
  static void startSending()
63
    {
64
    mRunning = true;
65

    
66
    if( mThis==null )
67
      {
68
      mList = new Vector<>();
69
      mThis = new EffectMessageSender();
70
      mThis.start();
71
      }
72
    else  
73
      {  
74
      synchronized(mThis)
75
        {
76
        mThis.notify();
77
        }
78
      }
79
    }
80

    
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82
  
83
  static void stopSending()
84
    {
85
    mRunning = false;
86

    
87
    if( mThis!=null )
88
      {
89
      synchronized(mThis)
90
        {
91
        mThis.notify();
92
        }
93
      }
94
    }
95
  
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97
  
98
  public void run()
99
    {
100
    Message tmp;  
101
     
102
    while(mRunning)
103
      {
104
      //android.util.Log.i("SENDER", "sender thread running...");
105

    
106
      while( mList.size()>0 )
107
        {
108
        tmp = mList.remove(0);
109
        tmp.mListener.effectMessage(tmp.mMessage, tmp.mEffectID, EffectNames.getName(tmp.mEffectName),tmp.mBitmapID);
110
        }
111

    
112
      synchronized(mThis)
113
        {
114
        if (!mNotify)
115
          {
116
          try  { mThis.wait(); }
117
          catch(InterruptedException ex) { }
118
          }
119
        mNotify = false;
120
        }
121
      }
122

    
123
    mThis = null;
124
    mList.clear();
125

    
126
    //android.util.Log.i("SENDER", "sender thread finished...");
127
    }
128
  
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130
        
131
  static void newMessage(EffectListener l, EffectMessage m, long id, int name, long bmpID)
132
    {
133
    Message msg = mThis.new Message(l,m,id,name,bmpID);
134
    mList.add(msg);
135

    
136
    synchronized(mThis)
137
      {
138
      mNotify = true;
139
      mThis.notify();
140
      }
141
    }
142
  }
143
///////////////////////////////////////////////////////////////////////////////////////////////////
(10-10/20)