project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/ChatlogAdapter.java
author Wuzzy <Wuzzy2@mail.ru>
Sat, 21 Oct 2017 18:38:28 +0200
changeset 12734 4e0e59255856
parent 10017 de822cd3df3a
permissions -rw-r--r--
Sniper rifle mission: Faster dynamite blow-up, also skip with precise
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     1
/*
7584
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     2
 * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     3
 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     4
 *
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     5
 * This program is free software; you can redistribute it and/or
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     6
 * modify it under the terms of the GNU General Public License
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     7
 * as published by the Free Software Foundation; either version 2
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     8
 * of the License, or (at your option) any later version.
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     9
 *
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    13
 * GNU General Public License for more details.
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    14
 *
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    15
 * You should have received a copy of the GNU General Public License
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    16
 * along with this program; if not, write to the Free Software
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    18
 */
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    19
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    20
package org.hedgewars.hedgeroid;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    21
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    22
import java.util.ArrayList;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    23
import java.util.Collection;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    24
import java.util.List;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    25
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    26
import org.hedgewars.hedgeroid.netplay.MessageLog;
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    27
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    28
import android.content.Context;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    29
import android.text.method.LinkMovementMethod;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    30
import android.view.View;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    31
import android.view.ViewGroup;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    32
import android.widget.AbsListView.LayoutParams;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    33
import android.widget.BaseAdapter;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    34
import android.widget.TextView;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    35
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    36
/**
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    37
 * Optimization: ListView is smart enough to try re-using the same view for an item
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    38
 * with the same ID, but it still calls getView for those items when the list changes.
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    39
 * Since lines with a given ID never change in our chatlog, we can avoid the effort
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    40
 * of TextView.setText in many cases by checking if the view is already set up for the
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    41
 * line with the right ID - but to do that, the view needs to remember the ID it's
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    42
 * holding the text for. That's what the LoglineView does.
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    43
 */
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    44
class LoglineView extends TextView {
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    45
    long chatlogId = -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    46
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    47
    public LoglineView(Context context) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    48
        super(context);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    49
    }
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    50
}
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    51
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    52
/**
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    53
 * For performance reasons, the chatlog is implemented as ListView instead of a
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    54
 * single TextView (although I later learned that TextView might also have
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    55
 * facilities for efficient appending with limited backlog... oh well, this
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    56
 * works). Every chat line is a line in the ListView, and this adapter prepares
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    57
 * the textviews from a messagelog in an efficient way.
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    58
 */
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    59
public class ChatlogAdapter extends BaseAdapter implements MessageLog.Listener {
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    60
    long idOffset = 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    61
    private List<CharSequence> log = new ArrayList<CharSequence>();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    62
    private Context context;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    63
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    64
    public ChatlogAdapter(Context context) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    65
        this.context = context;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    66
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    67
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    68
    public int getCount() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    69
        return log.size();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    70
    }
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    71
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    72
    public Object getItem(int position) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    73
        return log.get(position);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    74
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    75
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    76
    public long getItemId(int position) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    77
        return position+idOffset;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    78
    }
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    79
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    80
    public boolean hasStableIds() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    81
        return true;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    82
    }
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    83
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    84
    public void clear() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    85
        idOffset += log.size();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    86
        log.clear();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    87
        notifyDataSetChanged();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    88
    }
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    89
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    90
    public void lineAdded(CharSequence text) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    91
        log.add(text);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    92
        notifyDataSetChanged();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    93
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    94
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    95
    public void lineRemoved() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    96
        log.remove(0);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    97
        idOffset += 1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    98
        notifyDataSetChanged();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    99
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   100
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   101
    public void setLog(Collection<CharSequence> log) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   102
        idOffset += log.size();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   103
        this.log = new ArrayList<CharSequence>(log);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   104
        notifyDataSetChanged();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   105
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   106
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   107
    public View getView(int position, View convertView, ViewGroup parent) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   108
        LoglineView v = (LoglineView)convertView;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   109
        if (v == null) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   110
            v = new LoglineView(context);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   111
            v.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   112
            v.setMovementMethod(LinkMovementMethod.getInstance());
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   113
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   114
        long id = getItemId(position);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   115
        if(id != v.chatlogId) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   116
            v.setText(log.get(position));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   117
            v.chatlogId = id;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   118
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   119
        return v;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   120
    }
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   121
}