content: /*
   * Copyright 2019 ACINQ SAS
   *
   * 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.
   */
  
  package fr.acinq.eclair.wallet.activities;
  
  import android.content.ClipData;
  import android.content.ClipboardManager;
  import android.content.Context;
  import android.content.Intent;
  import androidx.databinding.DataBindingUtil;
  import android.os.Bundle;
  import androidx.appcompat.app.ActionBar;
  import androidx.appcompat.widget.Toolbar;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.Toast;
  
  import org.bouncycastle.util.encoders.Hex;
  import org.greenrobot.eventbus.EventBus;
  import org.greenrobot.eventbus.Subscribe;
  import org.greenrobot.eventbus.ThreadMode;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  
  import fr.acinq.bitcoin.ByteVector32;
  import fr.acinq.eclair.wallet.R;
  import fr.acinq.eclair.wallet.adapters.LocalChannelItemHolder;
  import fr.acinq.eclair.wallet.databinding.ActivityChannelRawDataBinding;
  import fr.acinq.eclair.wallet.events.ChannelRawDataEvent;
  import scodec.bits.ByteVector;
  
  public class ChannelRawDataActivity extends EclairActivity {
  
    private final Logger log = LoggerFactory.getLogger(ChannelRawDataActivity.class);
    private ActivityChannelRawDataBinding mBinding;
    private String mChannelId;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      mBinding = DataBindingUtil.setContentView(this, R.layout.activity_channel_raw_data);
  
      Toolbar toolbar = findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
      ActionBar ab = getSupportActionBar();
      ab.setDisplayHomeAsUpEnabled(true);
      Intent intent = getIntent();
      mChannelId = intent.getStringExtra(LocalChannelItemHolder.EXTRA_CHANNEL_ID);
      mBinding.rawJson.setHorizontallyScrolling(true);
    }
  
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handleRawDataEvent(ChannelRawDataEvent event) {
      if (event == null || event.json == null) {
        mBinding.rawJson.setText(getString(R.string.rawdata_error));
      } else {
        log.info("retrieved channel raw data {}", event.json);
        mBinding.rawJson.setText(event.json);
      }
    }
  
    @Override
    public void onResume() {
      super.onResume();
      if (checkInit()) {
        if (!EventBus.getDefault().isRegistered(this)) {
          EventBus.getDefault().register(this);
        }
        app.getLocalChannelRawData(ByteVector32.apply(ByteVector.view(Hex.decode(mChannelId))));
      }
    }
  
    @Override
    protected void onPause() {
      EventBus.getDefault().unregister(this);
      super.onPause();
    }
  
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
        case android.R.id.home:
          onBackPressed();
          return true;
        default:
          return super.onOptionsItemSelected(item);
      }
    }
  
    public void copyRawData(View v) {
      try {
        ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        clipboard.setPrimaryClip(ClipData.newPlainText("Channel data", mBinding.rawJson.getText().toString()));
        Toast.makeText(this.getApplicationContext(), getString(R.string.rawdata_copy_success), Toast.LENGTH_SHORT).show();
      } catch (Exception e) {
        Toast.makeText(this.getApplicationContext(), getString(R.string.rawdata_copy_error), Toast.LENGTH_SHORT).show();
      }
    }
  }
  