Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Android example for code that needs to change settings (Manifest.permission.WRITE_SETTINGS), targetSdkVersion >= 23
/**
* Created by spandhare on 11/3/16.
*/
import com.malmstein.fenster.controller.FensterPlayerControllerVisibilityListener;
import com.malmstein.fenster.play.FensterVideoFragment;
import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.RuntimePermissions;
@RuntimePermissions
public class GestureMediaPlayerActivity extends AppCompatActivity implements FensterPlayerControllerVisibilityListener {
Intent starterintent;
private static final int CODE_WRITE_SETTINGS_PERMISSION = 111;
boolean flag_is_permission_set = false;
@Override
@NeedsPermission(Manifest.permission.WRITE_SETTINGS)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
starterintent = getIntent();
if (!Settings.System.canWrite(this)) {
flag_is_permission_set = false;
Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
intent.setData(Uri.parse("package:" + this.getPackageName()));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityForResult(intent, GestureMediaPlayerActivity.CODE_WRITE_SETTINGS_PERMISSION);
} else {
flag_is_permission_set = true;
setContentView(R.layout.activity_gesture_media_player);
initVideo();
}
}
private void initVideo(){
findVideoFragment().setVisibilityListener(this);
findVideoFragment().playExampleVideo();
}
private FensterVideoFragment findVideoFragment(){
return (FensterVideoFragment) getFragmentManager().findFragmentById(R.id.play_demo_fragment);
}
@Override
public void onControlsVisibilityChange(boolean value) {
if (flag_is_permission_set) {
setSystemUiVisibility(value);
}
}
private void setSystemUiVisibility(final boolean visible) {
int newVis = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
if (!visible) {
newVis |= View.SYSTEM_UI_FLAG_LOW_PROFILE
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
}
final View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(newVis);
decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(final int visibility) {
if ((visibility & View.SYSTEM_UI_FLAG_LOW_PROFILE) == 0) {
findVideoFragment().showFensterController();
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CODE_WRITE_SETTINGS_PERMISSION && Settings.System.canWrite(this)){
Log.d("TAG", "CODE_WRITE_SETTINGS_PERMISSION success");
finish();
startActivity(starterintent);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment