Android Open Source - MakeWithMotoSampleApp Debug Fragment






From Project

Back to project page MakeWithMotoSampleApp.

License

The source code is released under:

GNU General Public License

If you think the Android project MakeWithMotoSampleApp 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

package com.makewithmoto.fragments;
/*from  w w w . j  a  va2  s .  c  om*/
import java.util.ArrayList;
import java.util.TreeSet;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.makewithmoto.example.ActivityMAKr;
import com.makewithmoto.example.R;

@SuppressLint("ShowToast")
public class DebugFragment extends Fragment {

  private String TAG = "qq";
  private FragmentActivity c;
  ListView lv;
  View v;
  ArrayList<Info> list = new ArrayList<Info>();
  public CustomAdapter adapter;

  public DebugFragment() {
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);

    v = inflater.inflate(R.layout.fragment_debug, container, false);

    return v;
  }

  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    c = (FragmentActivity) getActivity();
    adapter = new CustomAdapter(getActivity(), list);

    setRetainInstance(true);

    lv = (ListView) v.findViewById(R.id.logWindow);
    lv.setAdapter(adapter);
    lv.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
    lv.setStackFromBottom(true);

    final EditText cmdEditText = (EditText) v.findViewById(R.id.pdName);

    Button btn = (Button) v.findViewById(R.id.pdSend);
    btn.setOnClickListener(new OnClickListener() {

      public void onClick(View v) {
        String cmd = cmdEditText.getText().toString();
        Log.d(TAG, cmd);

        //TOFIX write to serial 
        ((ActivityMAKr) c).makr.writeSerial(cmd);
        adapter.addLeftItem(cmd);

      }
    });

    ToggleButton toggleButton = (ToggleButton) v
        .findViewById(R.id.lockList);
    toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {

      @Override
      public void onCheckedChanged(CompoundButton buttonView,
          boolean isChecked) {
        if (isChecked) {
          lv.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
        } else {
          lv.setTranscriptMode(ListView.TRANSCRIPT_MODE_DISABLED);
        }

      }
    });
    toggleButton.setChecked(true);
    
  }

  public void bindButton(int resource, final int value) {
    Button btn = (Button) v.findViewById(resource);
    btn.setSoundEffectsEnabled(false);

    btn.setOnTouchListener(new OnTouchListener() {
      public boolean onTouch(View v, MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_DOWN) {
          // Log.d("qq", "action_down");

        } else {

        }
        return false;
      }
    });

  }

  private Toast toast = null;

  private void toast(final String msg) {
    getActivity().runOnUiThread(new Runnable() {

      @Override
      public void run() {
        if (toast == null) {
          toast = Toast.makeText(getActivity()
              .getApplicationContext(), "", Toast.LENGTH_SHORT);
        }
        toast.setText(TAG + ": " + msg);
        toast.show();
      }
    });
  }

  public void clear() {
    list.clear();
    adapter.notifyDataSetChanged();

  }

  public class CustomAdapter extends BaseAdapter {

    private static final int TYPE_ITEM = 0;
    private static final int TYPE_SEPARATOR = 1;
    private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;

    ArrayList<Info> list_;
    private TreeSet mSeparatorsSet = new TreeSet();

    public CustomAdapter(Context context, ArrayList<Info> list) {
      super();

      list_ = list;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      View v = convertView;
      ViewHolder holder;
      // L.d("view", "getting view");
      int type = getItemViewType(position);
      // Log.d("view", "" + type);

      if (v == null) {
        LayoutInflater vi = (LayoutInflater) getActivity()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        holder = new ViewHolder();

        switch (type) {
        case TYPE_ITEM:
          v = vi.inflate(R.layout.view_textview_receive, null);

          break;

        case TYPE_SEPARATOR:
          v = vi.inflate(R.layout.view_textview_send, null);

          break;

        default:
          break;
        }

        holder.content = (TextView) v.findViewById(R.id.content);
        v.setTag(holder);
      } else {
        holder = (ViewHolder) v.getTag();
      }

      Info info = (Info) list_.get(position);
      if (info != null) {
        holder.content.setText(info.content);
        // holder.item2.setText(custom.getSecond());
      }

      return v;
    }

    public void addLeftItem(final String text) {
      if (getActivity() != null) {
        getActivity().runOnUiThread(new Runnable() {

          @Override
          public void run() {
            list.add(new Info(text));
            adapter.notifyDataSetChanged();
          }
        });
      }
    }

    public void addRightItem(final String item) {
      if (getActivity() != null) {
        getActivity().runOnUiThread(new Runnable() {

          @Override
          public void run() {
            list_.add(new Info(item));
            // save separator position
            mSeparatorsSet.add(list_.size() - 1);
            notifyDataSetChanged();
          }
        });
      }
    }

    @Override
    public int getItemViewType(int position) {
      return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR
          : TYPE_ITEM;
    }

    @Override
    public int getViewTypeCount() {
      return TYPE_MAX_COUNT;
    }

    @Override
    public int getCount() {
      return list_.size();
    }

    @Override
    public Object getItem(int position) {
      return list_.get(position);
    }

    @Override
    public long getItemId(int position) {
      return position;
    }

    @Override
    public boolean hasStableIds() {
      return true;
    }

  }

  class Info {
    String content;

    public Info(String soundName) {
      this.content = soundName;

    }
  }

  private class ViewHolder {
    TextView content;
  }

}




Java Source Code List

android_serialport_api.SerialPort.java
com.makewithmoto.boards.MAKr.java
com.makewithmoto.boards.MIOIOListener.java
com.makewithmoto.bt.BluetoothSerialService.java
com.makewithmoto.bt.BluetoothViewer.java
com.makewithmoto.bt.DeviceListActivity.java
com.makewithmoto.bt.SimpleBT.java
com.makewithmoto.example.ActivityBT.java
com.makewithmoto.example.ActivityMAKr.java
com.makewithmoto.example.ActivityMOIO.java
com.makewithmoto.example.Application.java
com.makewithmoto.example.LauncherActivity.java
com.makewithmoto.extras.AccelerometerManager.java
com.makewithmoto.extras.CustomSensorListener.java
com.makewithmoto.extras.CustomSensorManager.java
com.makewithmoto.extras.WhatIsRunningInterface.java
com.makewithmoto.extras.WhatIsRunning.java
com.makewithmoto.fragments.CameraFragment.java
com.makewithmoto.fragments.DebugFragment.java
com.makewithmoto.fragments.EmptyFragment.java
com.makewithmoto.fragments.GraphFragment.java
com.makewithmoto.fragments.ImageFragment.java
com.makewithmoto.fragments.Utils.java
com.makewithmoto.fragments.VideoPlayerFragment.java
com.makewithmoto.fragments.VideoTextureFragment.java
com.makewithmoto.views.CanvasUtils.java
com.makewithmoto.views.PlotView.java