Android Open Source - Android_NFC_FelicaEdit Service List






From Project

Back to project page Android_NFC_FelicaEdit.

License

The source code is released under:

Apache License

If you think the Android project Android_NFC_FelicaEdit listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.//w ww . j a va  2 s.c om
 */


package jp.co.yumemi.rd.felicaedit;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jp.co.yumemi.nfc.FelicaTag;
import jp.co.yumemi.nfc.NfcException;
import jp.co.yumemi.nfc.TagFactory;
import jp.co.yumemi.nfc.FelicaTag.ServiceCode;
import jp.co.yumemi.nfc.FelicaTag.SystemCode;
import jp.co.yumemi.rd.misc.SimpleAlert;
import jp.co.yumemi.rd.misc.Util;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;

/**
 * ?????????????????????????????????????????Activity.
 * @author morishita_2
 */
public class ServiceList extends Activity implements ExpandableListView.OnChildClickListener{
    private List<ServiceCode> serviceCodeList;
    private FelicaTag felica;
    private SystemCode systemCode;
    
    List<List<Map<String, String>>> childData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.service);
        setTitle(R.string.service_list_title);
        Intent intent = getIntent();
        this.felica = (FelicaTag)TagFactory.create(intent);
        this.systemCode = new SystemCode(intent.getByteArrayExtra(SystemCode.class.getCanonicalName()));
        // ExpandableListView ?????????????????????????????: see http://d.hatena.ne.jp/rudi/20100720/1279632918
        ExpandableListView listView = (ExpandableListView)findViewById(R.id.service_list);
        TextView tv = new TextView(this); // HeaderView?
        listView.addHeaderView(tv);
        try {
            felica.polling(systemCode);
            tv.setText(String.format("%s\nIDm: %s", systemCode.toString(), felica.getIdm().simpleToString()));
            this.serviceCodeList = felica.getServiceCodeList();
            List<Map<String, String>> groupData = new ArrayList<Map<String,String>>(); // ???????
            childData = new  ArrayList<List<Map<String,String>>>(); // ????????
            if (serviceCodeList.isEmpty()) {
                new SimpleAlert(this).show("??????????????????????????????????????????????????", true);
                return;
            }
            for (ServiceCode sc : serviceCodeList) {
                Map<String, String>groupMap = new HashMap<String, String>();
                List<Map<String, String>> children = new ArrayList<Map<String, String>>(); // ??????????????????
                String groupLabel = sc.toString();
                if (!sc.encryptNeeded()) {
                    for (int addr=0; ;addr++) {
                        Map<String, String> m = createBlockDataObject(sc, addr);
                        if (m == null) {
                            break;
                        }
                        children.add(m);
                    }
                    groupLabel = String.format("%s(%d)", groupLabel, children.size());
                } 
                groupMap.put("service_code", groupLabel);
                groupData.add(groupMap); // ?????????
                childData.add(children); // ??????????
            }
            ExpandableListAdapter adapter = new SimpleExpandableListAdapter(
                    getApplicationContext(),
                    groupData,
                    android.R.layout.simple_expandable_list_item_1,
                    new String[]{"service_code"},
                    new int[]{android.R.id.text1},
                    childData,
                    android.R.layout.simple_expandable_list_item_2,
                    new String[] {"addr", "data"},
                    new int[]{android.R.id.text1, android.R.id.text2}
                    );
            listView.setAdapter(adapter);
            listView.setOnChildClickListener(this);
        } catch (NfcException e) {
            new SimpleAlert(this).show("??????????????????????", true);
        }
    }
    
    private Map<String, String> createBlockDataObject(ServiceCode sc, int addr) throws NfcException {
        byte[] blockdata = felica.readWithoutEncryption(sc, addr); // read FeliCa Block Data
        if (blockdata == null) {
            return null;
        }
        // ??????????
        Map<String, String> curChildMap = new HashMap<String, String>();
        curChildMap.put("addr", String.format("Block %02d", addr));
        curChildMap.put("data", String.format("%s", Util.getHexString(blockdata)));
        return curChildMap;
    }
    
    private int editGroupPosition; // ?(ServiceCode) ??????
    private int editChildPosition; // ?????????????????(Block No)
    /**
     * ???????????????????Tap???????????????? Activity????????????
     */
    @Override
    public boolean onChildClick(ExpandableListView parent, View v,
            int groupPosition, int childPosition, long id) {
        ServiceCode serviceCode = serviceCodeList.get(groupPosition);
        if (serviceCode.isWritable() && !serviceCode.encryptNeeded()) {
            Intent intent = new Intent(ServiceList.this, EditBlock.class);
            felica.putTagService(intent);
            intent.putExtra(SystemCode.class.getCanonicalName(), systemCode.getBytes());
            intent.putExtra(ServiceCode.class.getCanonicalName(), serviceCode.getBytes());
            intent.putExtra(EditBlock.BLOCK_INDEX, childPosition);
            this.editGroupPosition = groupPosition;
            this.editChildPosition = childPosition;
            startActivityForResult(intent, 0);
        } else {
            (new SimpleAlert(this)).show("?????????????ReadOnly???????????????????????", false);
        }
        return false;
    }
    
    /**
     * startActivityForResult ?????????????? Activity??????????????????????????????
     * ???????????????????????????????????????
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        List<Map<String, String>> children = childData.get(editGroupPosition);
        try {
            Map<String, String> b = createBlockDataObject(serviceCodeList.get(editGroupPosition), editChildPosition);
            children.set(editChildPosition, b);
            ((ExpandableListView)findViewById(R.id.service_list)).invalidateViews();
        } catch (NfcException e) {
        }
    }
}




Java Source Code List

jp.co.yumemi.nfc.FelicaTag.java
jp.co.yumemi.nfc.NFCUtil.java
jp.co.yumemi.nfc.NfcException.java
jp.co.yumemi.nfc.NfcTag.java
jp.co.yumemi.nfc.PollingResponse.java
jp.co.yumemi.nfc.ReadResponse.java
jp.co.yumemi.nfc.TagFactory.java
jp.co.yumemi.rd.felicaedit.EditBlock.java
jp.co.yumemi.rd.felicaedit.FelicaEdit.java
jp.co.yumemi.rd.felicaedit.ServiceList.java
jp.co.yumemi.rd.felicaedit.SystemList.java
jp.co.yumemi.rd.misc.SimpleAlert.java
jp.co.yumemi.rd.misc.Util.java