Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package com.instreamatic.adman.demo.voice;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import com.instreamatic.adman.*;
import com.instreamatic.adman.view.DefaultAdmanView;
import com.instreamatic.adman.voice.AdmanVoice;
public class DemoAdmanVoiceActivity extends AppCompatActivity implements Adman.Listener {
private IAdman adman;
private View mLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo_adman_voice_activity);
mLayout = findViewById(R.id.main_layout);
checkPermission();
AdmanRequest request = new AdmanRequest.Builder()
.setSiteId(777)
.setRegion(Region.EUROPE)
.setType(Type.VOICE)
.build();
adman = new Adman(this, request);
adman.bindModule(new DefaultAdmanView(this));
adman.bindModule(new AdmanVoice(this) {
// ToDo: for debug
@Override
protected void onTranscriptionUpdate(String text) {
super.onTranscriptionUpdate(text);
((TextView) findViewById(R.id.text)).setText(text);
}
});
adman.setListener(this);
findViewById(R.id.start).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
adman.start();
}
});
}
@Override
public void onAdmanEvent(AdmanEvent event) {
}
final private static int PERMISSIONS_REQUEST_RECORD_AUDIO = 1;
private void checkPermission() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.RECORD_AUDIO)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.RECORD_AUDIO)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
Snackbar.make(mLayout, R.string.permission_record_audio_rationale,
Snackbar.LENGTH_INDEFINITE)
.setAction(R.string.ok, new View.OnClickListener() {
@Override
public void onClick(View view) {
ActivityCompat.requestPermissions(DemoAdmanVoiceActivity.this,
new String[]{Manifest.permission.RECORD_AUDIO},
PERMISSIONS_REQUEST_RECORD_AUDIO);
}
})
.show();
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.RECORD_AUDIO},
PERMISSIONS_REQUEST_RECORD_AUDIO);
// PERMISSIONS_REQUEST_RECORD_AUDIO is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSIONS_REQUEST_RECORD_AUDIO: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.